Thursday, December 31, 2009

Some Regular Expressions for Validations

Expression
^\d{1,2}\/\d{1,2}\/\d{4}$

Description
This regular expressions matches dates of the form XX/XX/YYYY where XX can be 1 or 2 digits long and YYYY is always 4 digits long.

Matches
4/1/2001 | 12/12/2001 | 55/5/3434

Non-Matches
1/1/01 | 12 Jan 01 | 1-1-2001

----------------------------------------------------------------------------------------------------------
Expression
^([0-1][0-9]|[2][0-3]):([0-5][0-9])$

Description
Matches a string if it is a valid time in the format of HH:MM

Matches
02:04 | 16:56 | 23:59

Non-Matches
02:00 PM | PM2:00 | 24:00
----------------------------------------------------------------------------
Expression
^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?)$

Matches
name.surname@blah.com | Name Surname | "b. blah"@blah.co.nz

Non-Matches
name surname@blah.com | name."surname"@blah.com | name@bla-.com

 -----------------------------------------------------------------
 Expression
^([\w\d\-\.]+)@{1}(([\w\d\-]{1,67})|([\w\d\-]+\.[\w\d\-]{1,67}))\.(([a-zA-Z\d]{2,4})(\.[a-zA-Z\d]{2})?)$

 Description
This pattern allows standard e-mail addresses (e.g. user@domain.com), sub domains (e.g. user@foo.domain.com), the new two- and four-letter domains (e.g. user@domain.tv and user@domain.name) and country codes (e.g. user@foo.com.us). Also, this patter follows the Network Solutions standard length of 67 characters for top-level domains. The reason I allow numbers to be entered in the domain suffix is for future planning. If you do not want numbers to be able to be added as a domain suffix (e.g. user@domain.123), simply delete the last two occurrences of "\d".

Matches
foo@foo.com | foo@foo-foo.com.au | foo@foo.foo.info

 Non-Matches
foo@.com | foo@foo..com | foo@me@.com



No comments:

Popular Posts