Hi All!!
Within Power Pages I am trying to set the attach a file as required on a basic form when a Change Request = Bank Account Information with JavaScript.
I have tried the following with JavaScript trying to set adx_attachfilerequiredfield as true but cannot get it to work. Any ideas for the script would be appreciated!
@GWham1 with validators it is probably better to add them first, and then put the logic inside the validator e.g. field1 = "x" then .... if you add the validator inside an onchange then you also need to either remove the validator or check for its existence to avoid the same validator getting added multiple times (e.g. when someone toggles the field the onchange is on) - when this happens the each validator object gets called and the user sees multiple of the same error messages.
Something like the following might work. You will need to call the addValidator function with your file field ID when the change request is selected.
$(document).ready(function() {
// trigger on change?
$("#changerequest").click(function() {
if ($(this).val('Bank Account')) {
// set mandatory
addValidator ("gw_file", "Select a file");
}
});
// add validator
function addValidator(fieldName, fieldLabel) {
if (typeof (Page_Validators) == 'undefined') return;
// Create new validator
$("#" + fieldName + "_label").parent().addClass("required");
var newValidator = document.createElement('span');
newValidator.style.display = "none";
newValidator.id = "RequiredFieldValidator" + fieldName;
newValidator.controltovalidate = "";
newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + " is a mandatory field.</a>";
newValidator.validationGroup = "";
newValidator.initialvalue = "";
newValidator.evaluationfunction = function () {
var value = $("#" + fieldName).val();
if (value == null || value == "") {
return false;
} else {
return true;
}
};
// Add the new validator to the page validators array:
Page_Validators.push(newValidator);
// Wire-up the click event handler of the validation summary link
$("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); });
}
});
Hi, you should add an actual form validation and not just set the class: Add custom JavaScript to a form | Microsoft Learn
I think you would need to bind the function to the change event of the choice field. You would have this in your main function:
function updateRequiredClass(); {
//retrieve value of fields
//define condition under which the dependent field should be required
}
$("#choice_field").change(function() {
updateRequiredClass();
});
Fubar
69
Super User 2025 Season 1
oliver.rodrigues
49
Most Valuable Professional
Jon Unzueta
43