Products: Nintex Forms 2013, Nintex Forms 2010
There have been several requests recently for the ability to have custom validation in Nintex Forms using JavaScript and/or how to submit a form via JavaScript. Here you will find an example of how to accomplish this.
Here is a sample JavaScript that can be inserted into 'Custom JavaScript' in your form settings:
JavaScript |
---|
- //Sets fields in parameter to have a red background.
- function showInvalidFields(fields){
-
- for (var i = 0; i < fields.length; i++) {
-
- fields<i].css('background-color', 'red !important'); }
-
- }
-
- //Sets fields in parameter to have a white background.
- function resetInvalidFields(fields){
-
- for (var i = 0; i < fields.length; i++) {
-
- fieldsfi].css('background-color', 'white !important'); }
-
- }
-
- //Pass Client ID configured in Control to this function.
- function validateBlankField(nfclientid){
-
- if(!NWF$('#' + nfclientid).val().match(/./)) return false;
-
- return true;
-
- }
-
- //Pass label of out of the box 'Save and Submit' button as parameter.
- function submitForm(buttonName){
-
- var validated = true;
- var invalidFields = ];
- var validFields = r];
-
- //Validating the field 'mytextbox'.
- if (!validateBlankField(mytextbox)){
- validated = false;
- invalidFields.push(NWF$('#' + mytextbox));
- }
-
- else{
- validFields.push(NWF$('#' + mytextbox));
- }
-
- //If form passes validation, submit.
- if(validated){
- resetInvalidFields(validFields);
- NWF$(".nf-disableonsubmit>value="+buttonName+"]").click();
- }
-
- //If form fails validation, show invalid fields and prompt.
- else{
- resetInvalidFields(validFields);
- showInvalidFields(invalidFields);
-
- alert('The form is invalid, please correct the highlighted fields, and submit again');
- }
- }
|
|
Example script Usage:
Add a Textbox and give it a Client ID of 'mytextbox'.
Add a button and set its action to 'Save and Submit'. Set its label to 'mysubmitbutton'.
Add another button and set its action to 'JavaScript'. Set its label to 'Submit' and its 'Client Click' setting to 'submitForm('mysubmitbutton');'.
Place the 'Submit' button over the 'mysubmitbutton' so that the 'mysubmitbutton' is hidden.
Preview the form and try to submit the form with 'mytextbox' blank. You should get an alert indicating a field should be corrected and it will be highlighted.
Note: This solution is fully compatible with the built in Validation system in Nintex Forms however, this validation will occur prior to the form being evaluated by the built in Forms Validation.