RegEx Validation: Cheatsheet of the most common regular expressions for validation (+ HowTo)

regex-validation-list

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)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(?<!\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)
(?<!\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)
(?<!\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)

Date in the format YYYY-MM-DD

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$
^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$
^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^[0-3]?[0-9][\/.][0-3]?[0-9][\/.](?:[0-9]{2})?[0-9]{2}$
^[0-3]?[0-9][\/.][0-3]?[0-9][\/.](?:[0-9]{2})?[0-9]{2}$
^[0-3]?[0-9][\/.][0-3]?[0-9][\/.](?:[0-9]{2})?[0-9]{2}$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^[0-3][0-9][\/.][0-3][0-9][\/.](?:[0-9][0-9])?[0-9][0-9]$
^[0-3][0-9][\/.][0-3][0-9][\/.](?:[0-9][0-9])?[0-9][0-9]$
^[0-3][0-9][\/.][0-3][0-9][\/.](?:[0-9][0-9])?[0-9][0-9]$

Date in M/D/YYY format

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$

Time in HH:MM format (24 hours format)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$

Time in HH:MM format (12 hours format)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(0?[1-9]|1[0-2]):[0-5][0-9]$
^(0?[1-9]|1[0-2]):[0-5][0-9]$
^(0?[1-9]|1[0-2]):[0-5][0-9]$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(0?[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9]$
^(0?[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9]$
^(0?[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9]$

Time in H:MM AM/PM format

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))$
^((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))$
^((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9]) ?([AaPp][Mm]))$
^((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9]) ?([AaPp][Mm]))$
^((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9]) ?([AaPp][Mm]))$

Validate password

Only numbers and letters (at least 8 characters)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&#\^]{12,}$

Validate phone numbers & mobile numbers

Recognizes most popular formats (different countries)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(\(?([\d \-\)\–\+\/\(]+)\)?([ .\-–\/]?)([\d]+))
(\(?([\d \-\)\–\+\/\(]+)\)?([ .\-–\/]?)([\d]+))
(\(?([\d \-\)\–\+\/\(]+)\)?([ .\-–\/]?)([\d]+))

Validate email address

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^([\w\-\.]+@(?!gmail.com)(?!hotmail.com)([\w\- ]+\.)+[\w-]{2,4})?$
^([\w\-\.]+@(?!gmail.com)(?!hotmail.com)([\w\- ]+\.)+[\w-]{2,4})?$
^([\w\-\.]+@(?!gmail.com)(?!hotmail.com)([\w\- ]+\.)+[\w-]{2,4})?$

For all domains

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^[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])?)*$
^[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])?)*$
^[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])?)*$

Validate URL

Only URLs with FTP protocol

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(ftp:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
^(ftp:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
^(ftp:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
^(http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$
^(http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$

All possible URL formats (including IP addresses and anchors)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$
^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
}
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 }
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
}

RegEx Validierung in PHP

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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
}
$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 }
$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
}

RegEx Validierung in Java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
}
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 }
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
}

What validation is still missing here? Feel free to write me a comment! 🙂

Related Posts
Join the Conversation

1 Comment

Your email address will not be published. Required fields are marked *

bold italic underline strikeThrough
insertOrderedList insertUnorderedList outdent indent
removeFormat
createLink unlink
code

This can also interest you