Pages

Monday 14 April 2014

Client side Validation using Regular Expression


Client side Validation using Regular Expression : 

There are situations where user may performs many types of validation
on single field for example while entering email user must check empty 
field as well as email syntax all such validations can be performed in
single line by creating regular expression. 

In javascript there is method called match() which compare regEx
with the input field value.

  var reg = /^[V|v][0-9]{3}$/;
            var x = $("#Version").val();
   if (!x.match(reg)) {
        alert("Version must start with character v followed by 3 digit number");
            }
         
In above code I compared regular expression which is stored in variable reg
with the value entered in the textbox whose id is Version. Then in if 
condition I compared the regEx and value.     

No comments:

Post a Comment