Collection of Regular Expression


Type: String

A. Description: This allows you to validate first names and last names in separate fields. Instead of validating a full name in one field.

Regx: ^[a-zA-Z]+(([\'\,\.\-][a-zA-Z])?[a-zA-Z]*)*$

Result: Mahesh | Deepak'P | Karthik-OK

B. Description: Used to verify that the string consists of only A-z, 0 - 9 and underscore. Several words allowed, only one space between them. No spaces up front. Hey, I know it's easy, but it was not in the library! Plus, the test option here is not working well all the time.

Regx: (\w(\s)?)+

Result: Mahesh | DeepakP | Karthik_OK

C. Description: UK National Insurance Number (NINo) validation. The following modifications have been made: The first letter may not be D, F, I, Q, U or Z; the second letter may not be D, F, I, O, Q, U or Z; the final letter is optional.

Regx: ^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$

Result: JG103759A | AP019283D | ZX047829C

Type: Numbers

A. Description: Matches positive whole numbers from 0-999

Regx: ^\d(\d)?(\d)?$

Result: 0 | 12 | 876

B. Description: validates to 5 digits and 2 decimal places but not allowing zero

Regx: (?!^0*$)(?!^0*\.0*$)^\d{1,5}(\.\d{1,2})?$

Result: 12345.12 | 0.5

C. Description: for checking a value is between 99.99% and 00.00%

Regx: ^((\d{1,2})?([.][\d]{1,2})?){1}[%]{1}$

Result: 99.99% | 9% | .09%

Type: Contact Numbers

A. Description: UK telephone number regex

Regx: (\s*\(?0\d{4}\)?(\s*|-)\d{3}(\s*|-)\d{3}\s*)|(\s*\(?0\d{3}\)?(\s*|-)\d{3}(\s*|- )\d{4}\s*)|(\s*(7|8)(\d{7}|\d{3}(\-|\s{1})\d{4})\s*)

Result: 0208 993 5689 | 0208-993-5689 | 89935689

B. Description: for 10 or 11 digit mobile number

Regx: ^[0-9]{10,11}$

Result: 09977552211| 8005577888

Type: Date and Times

A. Description: Matches month, requires that months 1-9 have a leading 0

Regx: ^((0[1-9])|(1[0-2]))$

Result: 01 | 02 | 12

B. Description: Simple American date format mm-dd-yyyy or mm-dd-yy, no time. Date range is 1900 - 2099.

Regx: ^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))$

Result: 02-29-2004 | 1/31/1997 | 1-2-03

C. Description: Matches variations on date/time/AM-PM. Must have 4 digit year, but everything else is open.

Restrictions are: 4 digit year, months 1-12, hours 1-23, minutes and seconds 1-59, any case of AM and PM

Regx: ^(([0]?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/[1-2]\d{3})? ?((([0-1]?\d)|(2[0-3])):[0-5]\d)?(:[0-5]\d)? ?(AM|am|PM|pm)?$

Result: 12/30/2002 | 12/30/2002 9:35 pm | 12/30/2002 19:35:02


Similar Articles