An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Wednesday, August 19, 2009

Regular Expression - Email, Date, Mobile Number, Postcode, Alphanumeric, Title

No comments :


Email - Regular Expression: /^[a-zA-Z0-9][a-zA-Z0-9\w\-\.\_]*@[a-zA-Z0-9\w\-\.\_]+\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/

The above regex is for email

Valid Values:
email@gmail.com
email@gmail.co.in
email@gmail.co.uk

Invalid Values:
@gmail.com
gmail.com
email@gmail.co.
----------------

India Postcode - Regular Expression: /^[0-9]{6}$/
The above regex is for indian postcode

Valid Values:
600073
600001

Invalid Values:
6000
1000
100

----------------

UK Postcode - Regular Expression:
UK Postcode - Regular Expression General Pattern (Without Space): /^[a-zA-Z0-9]{3,9}$/
UK Postcode - Regular Expression General Pattern (With Space): /^[a-zA-Z0-9 ]{3,9}$/
UK Postcode - Regular Expression - Pattern1: /^[a-zA-Z][a-zA-Z0-9][0-9]{3}$/
UK Postcode - Regular Expression - Pattern2: /^[a-zA-Z][a-zA-Z0-9][0-9][0-9]{4}$/

The above regex's are for valid uk postcode; you may have to use any/all of the patterns depending on your requirement

----------------

Alphanumeric - Regular Expression: /^[a-zA-Z0-9]*$/
The above regex is for valid alphanumeric (alpha numeric) inputs

Valid Values:
ALPHA123
ALP123HA
1ALP3HA

Invalid Values:
ALPHA*#@
#@ALPHA*
----------------

Alphanumeric (FIRST LETTER ALPHABET) - Regular Expression: /^[a-zA-Z][a-zA-Z0-9]*$/
The above regex is for valid alphanumeric (alpha numeric) inputs having first letter as alphabet

Valid Values:
ALPHA123
ALP123HA
1ALP3HA

Invalid Values:
ALPHA*#@
#@ALPHA*

----------------

Alphanumeric (With Spaces) - Regular Expression: /^[a-zA-Z0-9 ]*$/
The above regex is for valid alphanumeric input with spaces

Valid Values:
ALPH A123
ALP12 3HA
1ALP 3HA

Invalid Values:
ALPHA*#@
#@ALPHA*

----------------

Alphanumeric (With Spaces, Special Characters, Carriage Return, New Line) - Regular Expression: /^[a-zA-Z0-9\!\~\@\#\$\%\^\&\*\n\r ]*$/

The above regex is for valid alphanumeric with special characters; If you want to add more special characters? then add it with preceeding with a backslash (\)

Valid Values:
ALPH A123#@$
ALP12 3HA
1ALP 3HA
ALPHA*#@

----------------

DATE (FORMAT: YYYY/MM/DD) - Regular Expression: /^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}$/

The above regex is for valid date inputs (format: yyyy/mm/dd)

Valid Values:
2009/01/01
2009/11/31

Invalid Values:
2009/01
2009//
2009/12/
2009//11
----------------

DATE (FORMAT: DD/MM/YYYY) - Regular Expression: /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/

The above regex is for valid date inputs (format: dd/mm/yyyy)

Valid Values:
01/01/2009
11/31/2009

Invalid Values:
11/31/2009/
/02/2009/
11/2009

----------------

DATE (FORMAT: DD/MM/YYYY) - Regular Expression: /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/

The above regex is for valid date inputs (format: dd/mm/yyyy)

Valid Values:
01/01/2009
11/31/2009

Invalid Values:
11/31/2009/
/02/2009/
11/2009

----------------

Title (FORMAT: DD/MM/YYYY) - Regular Expression: /^MrMissMrsDrSir$/

The above regex is for valid title name

----------------

Indian - Mobile Number - Regular Expression:

Pattern1 - Mobile Number - Regular Expression: /^+?[0-9]{2}-?[0-9]{10}$/
e.g: +91-9392323323 or 919392323323

Pattern2 - Mobile Number - Regular Expression: /^0?[0-9]{10}$/
e.g: 09392323323 or 9392323323

Pattern3 - Mobile Number - Regular Expression: /^\+?\(?[0-9]{0,2}\)?\-?[0-9]{10}$/
e.g: (91)-9392323323 or 919392323323 or +91-9392323323

The above regex is for a valid indian mobile number

No comments :