DEV Community

Cover image for RegEx Validation: A Cheatsheet (most used)
webdeasy.de
webdeasy.de

Posted on • Originally published at webdeasy.de

RegEx Validation: A Cheatsheet (most used)

Regular expressions are often used to validate user input. These are repeated when validating the same input. Therefore I have created a list with the most important RegEx strings.


Just save this article and copy the corresponding regular expression into your next project where you need it.

For all RegEx validations there are infinite possibilities in the implementation. I have tested all the ones shown here extensively.

To test regular expressions more easily and not run into trial and error there are several tools online. I have linked my favorite RegEx Validator here:
RegEx Validator


Validate date & time

Date in the format YYYMMDD (0-9999 years)

(?<!\d)(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[1,3-9]|1[0-2])(?:29|30)))|(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))0229)|(?:(?:1[6-9]|[2-9]\d)?\d{2})(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8]))(?!\d)
Enter fullscreen mode Exit fullscreen mode

Date in the format YYYY-MM-DD

^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$
Enter fullscreen mode Exit fullscreen mode

Date in the format DD.MM.YYYY (without leading zeros)

^[0-3]?[0-9][\/.][0-3]?[0-9][\/.](?:[0-9]{2})?[0-9]{2}$
Enter fullscreen mode Exit fullscreen mode

Date in the format DD.MM.YYYY (with leading zeros)

^[0-3][0-9][\/.][0-3][0-9][\/.](?:[0-9][0-9])?[0-9][0-9]$
Enter fullscreen mode Exit fullscreen mode

Date in M/D/YYY format

^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
Enter fullscreen mode Exit fullscreen mode

Time in HH:MM format (24 hours format)

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$
Enter fullscreen mode Exit fullscreen mode

Time in HH:MM:SS format (24 hours format)

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
Enter fullscreen mode Exit fullscreen mode

Time in HH:MM format (12 hours format)

^(0?[1-9]|1[0-2]):[0-5][0-9]$
Enter fullscreen mode Exit fullscreen mode

Time in HH:MM:SS format (12 hours format)

^(0?[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9]$
Enter fullscreen mode Exit fullscreen mode

Time in H:MM AM/PM format

^((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))$
Enter fullscreen mode Exit fullscreen mode

Time in the format H:MM:SS AM/PM

^((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9]) ?([AaPp][Mm]))$
Enter fullscreen mode Exit fullscreen mode

Validate password

Only numbers and letters (at least 8 characters)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
Enter fullscreen mode Exit fullscreen mode

Numbers, upper and lower case letters (at least 12 characters)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$
Enter fullscreen mode Exit fullscreen mode

Validate phone numbers & mobile numbers

Recognizes most popular formats (different countries)

(\(?([\d \-\)\–\+\/\(]+)\)?([ .\-–\/]?)([\d]+))
Enter fullscreen mode Exit fullscreen mode

Validate email address

Only email addresses from specific domains (here *@gmail.com and @hotmail.com)

^([\w\-\.]+@(?!gmail.com)(?!hotmail.com)([\w\- ]+\.)+[\w-]{2,4})?$
Enter fullscreen mode Exit fullscreen mode

For all domains

^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
Enter fullscreen mode Exit fullscreen mode

Validate URL

Only URLs with FTP protocol

^(ftp:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Enter fullscreen mode Exit fullscreen mode

Standard URLs (only in the following format: http(s)://example.com/ , including ports)

^(http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
Enter fullscreen mode Exit fullscreen mode

All possible URL formats (including IP addresses and anchors)

^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
Enter fullscreen mode Exit fullscreen mode

Implementation in different programming languages

Validation in different programming languages always looks slightly different. However, in general it is identical.

In all examples you just replace the variable regex with one of the above and the stringToValidate should contain a string with the respective user input.

RegEx Validierung in JavaScript

let stringToValidate = "ZH*EnD?vv9Xf&y8"; // e.g. value from a textarea
let regex =
  /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$/; // attention: no quotes!
if (stringToValidate.match(regex)) {
  // valid input
} else {
  // invalid input
}
Enter fullscreen mode Exit fullscreen mode

RegEx Validierung in PHP

$stringToValidate = "ZH*EnD?vv9Xf&y8";
$regex = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$/"; // attention: add a slash at the begin and the end
if (preg_match($regex, $stringToValidate)) {
  // valid input
} else {
  // invalid input
}
Enter fullscreen mode Exit fullscreen mode

RegEx Validierung in Java

String stringToValidate = "ZH*EnD?vv9Xf$y8";
String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&#\\^]{12,}$";
if (stringToValidate.matches(regex)) {
  // valid input
} else {
  // invalid input
}
Enter fullscreen mode Exit fullscreen mode

What validation is still missing here? Feel free to write me a comment! πŸ™‚


➑️ On my blog you can find many more tutorials and articles!

Thanks for reading! ❀️

Discussion (3)

Collapse
darkain profile image
Vincent Milum Jr

It is NOT recommended to use regular expressions to validate things like URLs or especially email addresses. They miss far too much of the actual valid spec. Its especially troublesome considering that PHP has built in functions to do this already.

php.net/manual/en/function.filter-...

If you'd like an example of an actual RFC compliant regular expression for email address validation, check this out, and then please understand why it is a terrible idea to implement it :)

ex-parrot.com/~pdw/Mail-RFC822-Add...

Collapse
webdeasy profile image
webdeasy.de Author

Thanks for your comment! I will add a note with this hint. But for example: I have three different URL validations for different purposes. Only matching a specific protocol and stuff like that...hope you know what I mean :)

Collapse
8koi profile image
Hachikoi

or you could just use autoregex.xyz/