Regex Recipes
Since I went through the time to create these, I might as well document a few of them for myself and anyone else who might be looking for it. As I refine and author more, I’ll add them here.
Phone Number Validation
This would be useful to validate phone numbers of customers, clients or prospects. This only validates the 10 digit phone number that does not have parenthesis and dashes. Minor modifications would be necessary to validate with those formatting characters included. (via NANPA.)
^(8(?:00|88)|([2-9])(?!\2{2}|9[\d]|00|11)[\d]{2})([2-9&&[^5]][\d]{2}|(?:5(?!55)[\d]{2}))([\d]{4})$[UPDATE: Didn't like what I had before and this should be a smaller, more optimized, version.]
This regex covers:
- All ERC (Easily Recognizable Codes) which would include codes like 333,444,555, etc.
- Allowance for toll-free numbers (800,866,877,888, etc.)
- ERC Service codes 211,311,411,511,611,711,811,911.
- Unallocated and/or premium codes (200,300,400,500,700,900, etc.)
- Standard area codes from 201 to 989.
- Removal of 555 Central Office prefix.
If you want to give these a try there are a number of online utilities, but RegExr is one of my favorites.