I am using the code below to add and validation to fields on a create form depending on certain criteria, it works perfectly when only adding or removing the validation to a single field, but if I try doing it for two or more fields then it doesn't work, any ideas?
//eg. addValidator("customerid", "Customer")
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 = "casetypecode";
newValidator.controltovalidate = fieldName;
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); });
}
//eg. removeValidator("customerid")
function removeValidator(fieldName) {
$.each(Page_Validators, function (index, validator) {
if (validator.id == "RequiredFieldValidator" + fieldName) {
Page_Validators.splice(index, 1);
}
});
$("#" + fieldName + "_label").parent().removeClass("required");
}