Dream House

Courage to do something…

Regular Expression Patterns

Posted by Mohammad Sajjad Hossain on February 27th, 2008

From various sources I have collected the following regular expression patterns. They helped me a lot in front-end and back-end validation.

Alpha:

Pattern: ^[a-zA-Z_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, space ( ‘ ‘ ) and underscore ( ‘_’).

Alphanumeric:

Pattern: ^[a-zA-Z0-9_ ]+$
Description: Allows only ‘A-Z’, ‘a-z’, ‘0-9′, space ( ‘ ‘ ) and underscore ( ‘_’).

Email:

Pattern: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

Numeric:

Pattern: ^\d+$

Decimal:

Pattern: ^\d+(?:\.\d{0,2})?$

Date - 1:

Pattern: ^(0[1-9]|[1,2][0-9]|3[0-1])-(0[1-9]|1[0,1,2])-\d{4}$
Description: Date format DD-MM-YYYY

Date - 2:

Pattern: ^(0[1-9]|1[0,1,2])-(0[1-9]|[1,2][0-9]|3[0-1])-\d{4}$
Description: Date format MM-DD-YYYY

US Zip Code:

Pattern: ^\d{5}$

US Telephone Number:

Pattern: ^1?[\-]?\(?\d{3}\)?[\-]?\d{3}[\-]?\d{4}$

Color Code:

Pattern: ^#{1}?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$

Share/Save/Bookmark

5 Responses to “Regular Expression Patterns”

  1. Arafat Rahman Says:

    Thanks for the post. It covers common regular expression patterns.
    Here you can find more details about regular expression
    http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/

  2. Mohammad Sajjad Hossain Says:

    Thank you Arafat for providing the link. It is really helpful.

  3. Naseer Ahmad Mughal Says:

    Thanks both of you for such a nice information to share.

  4. Raihan Says:

    Here you can find millions - http://www.regexlib.com

  5. Mohammad Sajjad Hossain Says:

    @Thank you Naseer.

    @Raihan Bhai, thanks for the link. My intension was to provide some expressions which we need time to time.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">