PHP


Common Regular Expressions/Data Validation using RegEx
By Richard Clark
26-Jul-09
Views: 19449

10 common regular expression for data validation
 
10 Regular Expressions (Page 1 of 1)
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
 
ValidatesInvalidates
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.
 
ValidatesInvalidates
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.
 
ValidatesInvalidates
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.
 
ValidatesInvalidates
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]
 
ValidatesInvalidates
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}$
 
ValidatesInvalidates
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
 
ValidatesInvalidates
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.
 
ValidatesInvalidates
image1.jpg
image1.JPEG
design.png
layout.gif
design_png
design.jpg1
audio.mp3

Signed Integer


RegEx: ^(\+|-)?\d+$
Description: Matches a signed integer
 
ValidatesInvalidates
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
 
ValidatesInvalidates
23:59
0:59
00:59
1:00
1:0
1:60
2:2
24:00
 

Deleting duplicate entries from the database is tricky, there are different queries to find duplicates in the database, but removing duplicates from the database is not that easy. This little article will help you remove duplicate entries in a controlled way . Read More Remove duplicates from Database Using PHP

Comments
RayBez
[28-Jul-2009]
#1

Hi, Do you have a regex to test for a FQDN? Thanks

mksimo
[19-Apr-2010]
#2

Thank you man. The email regex work great!

univarsal
[21-Sep-2010]
#3

Good regexp patterns defined here. We use own OOP validation functionality, it is very good for us and several companys we collaborate with. There is a short article about our functionality http://vitana-group.com/article/php/validation. Can I add your regexp patterns there?

sam
[21-Jul-2011]
#4

hi, good work. i am looking for the pattern to be able validate email address only for a few domains. E.x: domain.com, domain2.net, domain3.org. no other email address allowed.

I was trying to edit this pattern but no luck.

^[a-z0-9_+-]+(.[a-z0-9_+-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*.([a-z]{2,4})$

Thanks,

sam

 

elerrycrist
[04-Jan-2012]
#5

nice work,it helps me alot on my current project thanks to you.. :)

Posting comments has been disabled temporarily