Regular Expression Patterns
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})?$
Any HTML Tag:
Pattern: <([^">]+(?:”[^”]+”)*[^>]+)>