View Full Version : Regular expression in javascript for validation

12-10-2010, 08:02 AM
Regular Expressions provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns.
Some of the symbols used in regular expressions are as follows;
1) Vertical Bar (|) This symbol is used to separate alternatives. Eg: rear|read can match both 'rear' and 'read'.
2) Parenthesis (()) Defines the scope and precedence of operators. Eg: rea(r|d) can match both 'rear' and 'read'.
3) Quantification Tokens There are a list of quantification tokens. Like:
a) ? This denotes that there may be one or zero instance of the symbol present. Eg: rea?d can match both 'read' and 'red'
b) * This denotes that there may be zero or more instance of the symbol present. Eg: rea*d can match all 'read','red','reaaad','reaad' etc.
c) + This denotes that there may be one or more instance of the symbol present. Eg: rea+d can match all 'read','reaaaad','reaaad','reaad' etc
These expression are of great use when we are checking for a particular pattern through our program in .Net/Javacript. Like it may be useful in checking for any presence of special characters in a username field. It may also come to use when we are validating a mobile number field against the entry of any alphanumeric value. If we know how to use this feature properly, then the time taken for validating several fields may reduce by
Let us now see that how we can validate several instances using Javascript Regular Expressions. Eg:
1) Validate against any blank values:
2) Validate against non words:
3) Validate against any blank values:
4) Trimming a string Removing its left and right blank spaces:
5) Validating an Email Address:
6) Validating a File Name:
To go through a thorough tutorial on this topic along with a Video Tutorial to undesrtand in a better way, please visit;

http://visiontechno.net/studymats/?cat=19 (http://visiontechno.net/studymats/?cat=19)

Philip M
12-10-2010, 04:47 PM
Far better tutorials are already on this site at



Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.