Skip to content

Instantly share code, notes, and snippets.

@mgks
Last active May 7, 2023 10:49
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 mgks/bbeb710e5d20036ef12d72d928ca42f8 to your computer and use it in GitHub Desktop.
Save mgks/bbeb710e5d20036ef12d72d928ca42f8 to your computer and use it in GitHub Desktop.
Validating string types using RegEx on JavaScript.
// let's validate some string with regex
- Credit Cards (Visa, Mastercard, American Express, Discover Card)
- Date (Check complex information for different months and leap years)
- Email
- HEX Color Code
- IP Address
- Password (With at least one uppercase & one lowercase letter, one digit, one special character, and atleast 8 characters long)
- Phone Number (For global numbers with/without country codes and separators like hyphens, dots, or spaces)
- URL Validation
// following regex lets you validate credit cards and their types (including Visa, Mastercard, American Express and Discover cards)
^(?:4[0-9]{12}(?:[0-9]{3})? // Visa
| 5[1-5][0-9]{14} // Mastercard
| 3[47][0-9]{13} // American Express
| 6(?:011|5[0-9]{2})[0-9]{12} // Discover
)$
// these regex patterns can be used to check valid date format or to check much complex information for different months and leap years
// option 1
^\d{4}-\d{2}-\d{2}$ // simply checks for YYYY-MM-DD format
// option 2 (advanced solution)
/^(?:(?:31(/)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(/)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/ // this regex pattern validates dates in the format of dd/mm/yyyy, including leap years and different month lengths
// these regex patterns validate an email address and check if it is in the correct format
// option 1
^[^\s@]+@[^\s@]+\.[^\s@]+$
// option 2
/^[\w-.]+@([\w-]+.)+[\w-]{2,4}$/
// this regex pattern lets you check for a valid hex color code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
// following regex validates IP addresses
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
// these regex patterns ensure that password has at least one uppercase letter, one lowercase letter, one digit, one special character, and atleast 8 characters long
// option 1
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
// option 2
/^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$/
// these regex patterns can be used to check valid phone numbers either for US or any specific country or the advanced solution for global numbers with/without country codes
// option 1
^\d{3}-\d{3}-\d{4}$ // checks for US format only
// option 2 (advanced solution)
/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/ // this regex pattern validates phone numbers and allows for various formats including with or without country code and with or without separators like hyphens, dots, or spaces
// this regex pattern checks if a string is a valid URL
// option 1
^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?$
// option 2
/^(http(s)?://)?[a-zA-Z0-9-.]+.[a-zA-Z]{2,}(:[0-9]{1,5})?(/\S*)?$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment