This is a list of 10 commonly used regular expressions. Building a regular expression for email and url is never easy and they are never 100% correct. There is a trade-off between simplicity and efficiency/accuracy when working with complex regular expressions like this. So the regular expressions for email and URL validation presented here are tried to be as simple as possible and at the same time accurate and effienct as well. All these regular expressions are tested in PHP only (using preg_match). To use these regular expressions in PHP, you can use the following syntax
<?
$email="programmer88@yahoo.com";
$regex="|^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$|";
if(preg_match($regex,$email)){
echo "Email is valid";
}
else{
echo "Email is not valid";
}
?>
|
Email Address
|
| RegEx: |
^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$ |
| Description: |
The pattern allows -,+,_ and . in email address and also allows multi level domains |
| |
| Validates | Invalidates |
programmer88@yahoo.com
smart.programmer@yahoo.com
some1@yahoo.co.uk
kim_nl@email.com
|
programmer..88@yahoo.com
abc*88@yahoo.com
programmer@yahoo
programmer@yahoo.abcdefghi
abc.xyz@hotmail.com@nl
|
|
Strong Password
|
| RegEx: |
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$ |
| Description: |
Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters. |
| |
| Validates | Invalidates |
mankind7
a12345678
master22
abc12345
|
mankind
12345678
master@22
abcdefgh
|
|
Match URL
|
| RegEx: |
^((ht|f)tp(s?))\://([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(/\S*)?$ |
| Description: |
The regex is used to match a URL of protocol (http,ftp,https). WWW is optional and a port number at the end is also optional.
|
| |
| Validates | Invalidates |
http://www.google.com
http://google.com
http://www.google.com.nl
http://www.myserver.com:1290
ftp://godaddy.com:8080
|
http:/www.google.com
www.google.com
http://www.google.com/webmasters
http://localhost:8080
ftp.google.com
|
|
Floating Point Number
|
| RegEx: |
^[-+]?[0-9]*\.?[0-9]+$ |
| Description: |
Matches a signed floating point number.
|
| |
| Validates | Invalidates |
12345
-12345
-.234
+1.244
|
123.456.99
1234+
123abc
|
|
Alphanumeric characters
|
| RegEx: |
[A-Za-z0-9] |
| Description: |
To detect alphanumeric characters (for new username/password validation). To detect non-alphanumeric characters use [^A-Za-z0-9] |
| |
| Validates | Invalidates |
mike124
smith
dairyman88
|
mike++
richard.123
abc_123
|
|
US Phone Number
|
| RegEx: |
^[2-9][0-9]{2}-[0-9]{3}-[0-9]{4}$ |
| Description: |
This expression matches a hyphen separated US phone number, of the form ANN-NNN-NNNN, where A is between 2 and 9 and N is between 0 and 9. If you are not using PHP ereg function, you could also use ^[2-9]\d{2}-\d{3}-\d{4}$
|
| |
| Validates | Invalidates |
212-456-6653
|
112-456-6653
12345678
012-212-3456
|
|
US ZIP + 4 Standard
|
| RegEx: |
^\d{5}(-\d{4})?$ |
| Description: |
Matches US ZIP codes + 4 Standard |
| |
| Validates | Invalidates |
11002-6653
|
223456987
1234-12345
|
|
Image File Extension
|
| RegEx: |
.*(\.[Jj][Pp][Gg]|\.[Gg][Ii][Ff]|\.[Jj][Pp][Ee][Gg]|\.[Pp][Nn][Gg]) |
| Description: |
This expression should work to validate that an uploaded file's extension is either jpg, jpeg, gif, or png.
|
| |
| Validates | Invalidates |
image1.jpg
image1.JPEG
design.png
layout.gif
|
design_png
design.jpg1
audio.mp3
|
|
Signed Integer
|
| RegEx: |
^(\+|-)?\d+$ |
| Description: |
Matches a signed integer |
| |
| Validates | Invalidates |
123
-123
+123
|
.123
123.0
123+90
|
|
Time in format h:mm and hh:mm
|
| RegEx: |
^([0-1]?[0-9]|[2][0-3]):([0-5][0-9])$ |
| Description: |
Accepts data of time in format h:mm and hh:mm |
| |
| Validates | Invalidates |
23:59
0:59
00:59
1:00
|
1:0
1:60
2:2
24:00
|
|
| |