Last active
December 1, 2024 08:06
-
-
Save mgks/bbeb710e5d20036ef12d72d928ca42f8 to your computer and use it in GitHub Desktop.
Validating different string types with RegEx in JavaScript.
This file contains hidden or 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
// 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 | |
)$ |
This file contains hidden or 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
// 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 (simply checks for YYYY-MM-DD format) | |
^\d{4}-\d{2}-\d{2}$ | |
// option 2 (advanced solution, this regex pattern validates dates in the format of dd/mm/yyyy, including leap years and different month lengths) | |
/^(?:(?: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 file contains hidden or 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
// 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 file contains hidden or 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
// this regex pattern lets you check for a valid hex color code | |
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ |
This file contains hidden or 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
// 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]?)$ |
This file contains hidden or 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
// 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,}$/ |
This file contains hidden or 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
// 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 (checks for US format only) | |
^\d{3}-\d{3}-\d{4}$ | |
// option 2 (advanced solution, 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) | |
/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/ |
This file contains hidden or 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
// 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