Trigger Validation Rules via Javascript

  • 14 July 2022
  • 3 replies
  • 10 views

Hi how to trigger validation rules via Javascript? I have tried using Page_ClientValidate() but it is not highlighting the fields not like when clicking the Save and Submit Button.



 



Thanks for your help.


3 replies

Userlevel 5
Badge +12

While the Page_ClientValidate() function does evaluate and run all of the rules, it doesn't handle any of the styling on the controls to indicate that they are in error. To get that working you'll need to use a function called ValidationSummaryOnSubmit that is invoked anytime an onSubmit event is triggered.



 



Example Code:



if (!Page_ClientValidate();) {

ValidationSummaryOnSubmit(NWF$(".nf-validation-summary").prop("validationGroup"));

}


 



Note: The ValidationSummaryOnSubmit function requires a validationGroup string that can be found as a property on the actual form element that is where all of the Error Text is located in red at the top of your form. This is why I am targeting that element using the ".nf-validation-summary" class, and getting that particular property to pass into the function 😉 

Any who, this should be easy enough to test. Let me know if it works. 



 

Userlevel 5
Badge +12

There was a small typo in the code above, but I can no longer edit my post. I had added an unneeded semi-colon after the (!Page_ClientValidate()). In the code example below is a corrected version:



if (!Page_ClientValidate()) {

ValidationSummaryOnSubmit(NWF$(".nf-validation-summary").prop("validationGroup"));

}


 

Userlevel 5
Badge +12

Also, if this solved your issue, please mark the post as the solution so it can go into the Solved category of posts. Thank you! 



 

Reply