 |

Basic Examples
Advanced Examples
Regular Expression Tips
Regular Expression - What is it?
A Regular Expression is the most powerful tool used for Pattern Matching and Substitution. It is associated with a lot of scripting languages including, Perl, PHP, ASP and CFM and client-side scripting languages such as JavaScript.
Important note: Validate Forms Regular Expression Form Check is based upon NETSCAPE 3.0 (Javascript 1.2) implementation. Therefore all such references including the metacharacter information is based upon this implementation.
A Regular Expression lets you build patterns using a set of special characters called metacharacters (click here to see the list of available metacharacters that you can use when constructing your Regular Expression); these patterns can then be compared to input gathered from a web form and depending on whether or not there's a match, an appropriate action can be taken, such as popping up an alert box.
Here's a snapshot of what a Validate Form Regular Expression looks like:

Regular Expression Input Field – Use this field to create your regular expression.
Important note: When constructing your own Regular Expression, please always remember to insert an extra escape character (backslash "\") when escaping metacharacters such as decimal characters "d" or word characters "w" etc. In the picture above the "d" character has an extra backslash before it. This is necessary because when Validate Form implements the regular expression, it uses Javascripts Constructor Object that requires that any special characters be escaped within the regular expression.
To search for characters such as an open bracket "(", open square bracket "[" etc, whilst still conforming to the rules of a Regular Expression, it is also necessary to escape these characters as well. However they must be double escaped?
For example "\\("
would allow you to search for the open bracket within the search string.
"\\[" or "\\^"
would allow you to search for an open bracket or carot character within the string.
Flags Select List – Select an optional flag (default is global) to which you want your Regular Expression filtered upon (see table below for a description of each of the flags).
| Flag |
Description |
| g |
global search, this will search the entire field (string of characters) and display your error message, if it does NOT find a matching pattern within the field |
| i |
ignore case, this will ignore the case (upper or lowercase) and display your error message, if it does NOT find a matching pattern within the field |
| gi |
global search, ignore case, will search the entire field (string of characters), ignoring the case and will display your error message, if it does NOT find a matching pattern within the field |
| g-rev |
global search, this will search the entire field (string of characters) and display your error message, if it does find a matching pattern with the field |
| i-rev |
ignore case, this will ignore the case (upper or lowercase) and display your error message, if it does find a matching pattern with the field |
| gi-rev |
global, ignore case, will search the entire field (string of characters) and ignore the case, and display your error message, if it does find a matching pattern within the field |
| rev |
Display your error message if it does find a matching pattern within the field |
| Regular Expression – Basic Examples |
Top of Page |

All this does is match the pattern "love" or "LOvE" in the text it's applied to. "gi" in the dropdown select list stands for global, ignore case and will match the pattern anywhere within the string, ignoring the case. Like many other things in life, it's simpler to get your mind around the pattern than the concept.
How about something a little more complex? Try this:

This would match the words "fool", "footsie" and "four-seater", both in lower and uppercase.
The "+" that you see above is the first of what are called "metacharacters" - these are characters that have a special meaning when used within a pattern. The "+" metacharacter is used to match one or more occurrence of the preceding character - in the example above, the letter "f" followed by one or more occurrence of the letter "o".
Similar to the "+" meta-character, we have "*" and "?" - these are used to match zero or more occurrences of the preceding character, and zero or one occurrence of the preceding character, respectively. So,

would match "easy", "egocentric" and "egg", again both globally and in any case
while

would match "winnie", "wimpy" "Wilson" and "William", though not "Wendy" or "Wolf".
In case all this seems a little too imprecise, you can also specify a range for the number of matches. For example, the regular expression

would match "jimmy" and "jimmmmmy!", but not "jim". The numbers in the curly braces represent the lower and upper values of the range to match; you can leave out the upper limit for an open-ended range match.
To match the opposite of any of the above examples you must select the "rev" option from the dropdown select list. This tells Validate Form that you wish to select the reverse of the pattern. For example,

would match anything other than "jimmy" or "jimmmmmy!" etc, on a global, ignore case bases.
|
| Regular Expression – Advanced Examples |
Top of Page |
Description: US Phone Number
Reg Exp: ^1?\\s*-?\\s*(\\d{3}|\\(\\s*\\d{3}\\s*\\))\\s*-?\\s*\\d{3}\\s*-?\\s*\\d{4}$
Flag: gi
Matches:
1-123-456-7890, 1 (123) 456 7980, 1 123 456 7890, (123) 456-7890, 123-456-7890, and so on, and makes sure that if one parenthesis is present both must be present
Description: 5 or 9 Digit Zip Code
Reg Exp: ^\\d{5}(-?\\d{4})?$
Flag: gi
Matches:
12345, 123451234, or 12345-1234
Description: IP Address
Reg Exp: ^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]| [0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$
Flag: gi
Matches: 255.255.255.255, and 0.0.0.0, but doesn't match 256.1.1.1 or 999.1.1.1
Description: String Doesn't Contain Certain Characters
Reg Exp: ^[^ab]*$
Flag: gi
Matches:
hello, eye, fred (any string that doesn't have "a" or "b" in it), but doesn't match bye
Description: UK Postal Codes
Reg Exp: ^[A-Za-z]{1,2}[\\d]{1,2}([A-Za-z])?\\s?[\\d][A-Za-z]{2}$
Flag: gi
Matches:
CF1 2AA and cf564fg but not a1234d or A12 77Y
Description: Percentage
Reg Exp: ^(0*100{1,1}\\.?((?<=\\.)0*)?%?$)|(^0*\\d{0,2}\\.?((?<=\\.)\\d*)?%?)$
Flag: gi
Matches: 0, 0.0, 99.9, 100.0, but excludes -1, 100.1, etc
Description: Custom Input (Solution to Question 3 FAQ http://www.validateform.com/faqs.asp#3faq)
Reg Exp: ^[a-z]+((\\d+(-|\\s?))|(\\d+)|(-|\\s?))[a-z]*$
Flag: gi
Matches:
John, John-G or john-g, John G, John123, John123 Ggg, note: the input must begin with a word, with an optional number, an optional one space or dash character and an optional word at the end.
Description: Currency by Vrajesh
Reg Exp: ^\\d+(.?\\d{2})?$
Flag: gi
Matches:
1231.90 and 23
But Doesn't Match: 123...90 etc
|
| Regular Expression – Tips |
Top of Page |
- Know what it is you want from the user
The easiest way to create your own Regular Expression is to first of all decide on what it is you want from the user? What sort of information you want to gather from them?
The best technique that I can give you here, is to take a piece of paper and draw a line down the middle of it dividing it up into two columns. In the first column write, Acceptable Input and in the Second column write Unacceptable Input. Then start filling in each of the columns, with all the different possibilities that you can come up with.
When your finished and you think you've covered everything, then your ready to start building your regular expression.
- Have a digital or printed copy of the metacharacter information running in your computers background when creating your Regular Expression. You will most definitely need it, because you'll be referring to it heaps!
- Write your Regular Expression in small chunks and test each chunk!
For example:
When I created this Regular Expression:
Description: Custom Input (Solution to Question 2 FAQ http://www.validateform.com/faqs.asp#3faq)
RegExp: ^[a-z]+((\\d+(-|\\s?))|(\\d+)|(-|\\s?))[a-z]*$
Flag: gi
Matches:
John, John-G or john-g, John G, John123, John123 Ggg, note: the input must begin with a word, with an optional number, an optional one space or dash character and an optional word at the end.
I followed all of the above steps and then started breaking it down into workable parts:
- ^[a-z]+ says that the input field must begin with a word consisting of one or more alphabetical characters
- (\\d+(-|\\s?)) is the start of 3 possible solutions of one set of possibilities. This string says that the next piece of information can be a whole number of any length e.g. 0 or 10000000 etc, and then a compulsory single "-" dash character or single space character
- | the pipe character is used to separate sets of information, it is the same as a logical OR in programming terms (see below table for an explanation of nested sets)

This says: Please evaluate this expression on the information contained within the first set (Set 1) of open and closing brackets, if that fails move onto the next set (Set 2), and if that fails try the last set (Set 3). If all three fail, then condition is said to be NOT satisfied, and will result in a negative or failed response.
- [a-z]*$ says that the end of the string can contain zero or more characters
- The last thing to remember when creating your own Regular Expression is that practice makes perfect! The more you practice, the easier it gets.
|
|
|

|
 |
 |
| Click here to purchase your copy now! |
|
|
 |
293 Users have rated it with an average rating of 7.5 out of 10
         
|
"Already got the Extension?" - Then please tell others what you think by rating it!
|
|
|
|