Skip to content

Instantly share code, notes, and snippets.

@avtarnanrey
Created February 24, 2020 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avtarnanrey/27cdcfdfb8a469c7bfd431130a97753b to your computer and use it in GitHub Desktop.
Save avtarnanrey/27cdcfdfb8a469c7bfd431130a97753b to your computer and use it in GitHub Desktop.
Regular expressions & common form helper methods
// Phone Number format 000-000-0000
export const formattedPhoneRegex = /^[0-9]\d{2}-\d{3}-\d{4}$/i;
// Email
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