Created
February 24, 2020 15:05
-
-
Save avtarnanrey/27cdcfdfb8a469c7bfd431130a97753b to your computer and use it in GitHub Desktop.
Regular expressions & common form helper methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Phone Number format 000-000-0000 | |
export const formattedPhoneRegex = /^[0-9]\d{2}-\d{3}-\d{4}$/i; | |
export const emailRegex = /^[a-z0-9`!#\$%&\*\+\/=\?\^\'\-_]+((\.)+[a-z0-9`!#\$%&\*\+\/=\?\^\'\-_]+)*@([a-z0-9]+([\-][a-z0-9])*)+([\.]([a-z0-9]+([\-][a-z0-9])*)+)+$/i; | |
// No Special Chars | |
export const noSpecialCharRegex = /^[a-zA-Z0-9]+$/i; | |
// Auto format the phone number | |
export function autoFormat(value: string) { | |
let newVal = filterNumbers(value); | |
newVal = newVal.substr(0, 10); // Limit entry to 10 numbers only. | |
return newVal.length === 10 ? newVal.slice(0, 3) + "-" + newVal.slice(3, 6) + "-" + newVal.slice(6) : newVal; | |
} | |
export function filterNumbers(value: string) { | |
return value.replace(/\D/g, ""); // return only numbers | |
} | |
// Get Array from Typescript Enum | |
export function mapEnum (enumerable: EnumType, fn: Function): any[] { | |
// get all the members of the enum | |
let enumMembers: any[] = Object.keys(enumerable).map((key: any) => enumerable[key]); | |
// now map through the enum values | |
return enumMembers.map(m => fn(m)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment