instant forms validation


Badge +5

I have a form that is used to request new computer names. I have some code that uses NWF jQuery to validate against all exisiting hostnames, but it seems to do the validation OnSubmit, not onBlur. I want to provide the user with feedback instantly that their hostname is not good because when the form is submitted I need to send one and only one email with the requested hostname.

 

Is there a way to get a form field to vvalidate onBlur, rather than on submit?


2 replies

Badge +4

Hi there,

 

The following will validate an input field when the focus changes from it:

NWF$(document).ready(function() {       // Select using a unique class name we have supplied, choose input child element.     NWF$(".validateMe input:first-child").change(function() {              // Check value of the input field and set style.         if(NWF$(this).val() != "boo") {             NWF$(this).css(                 { "border-color": "#FF0000",                      "border-width":"2px",                        "border-style":"solid"}             );         }         else {             NWF$(this).css(                 { "border-color": "#000",                      "border-width":"1px",                        "border-style":"solid"}             );         }              }); });  

 

In the above example we check the input field with the class 'validateMe' associated with it.  If the input field does not have a value of "boo" when changed then a red border is applied, a black border is applied when the input value equals "boo".

 

Ensure the input field has the unique css class so it can be located:

80462_pastedImage_3.png

 

Put the validation JavaScript straight into Settings > Custom JavaScript.

 

You would also need to validate this on form save as the above will not prevent the user form saving invalid items.

 

Hope this gets you started.

Badge +5

Thanks Berry!

Very helpful.

Reply