954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php regex for form validation

does anyone know of any good resources for php regex for form validation.

I am looking for good regex for

name: (including -.' and no spaces in the first space no doubles of ..''--

phone: again no spaces at the start allowing + and -

alpha: only lowercase letters no numbers no spaces allowing -_ but no doubles --__ or _-_-
alphaspace: same as above but with spaces allowing .',-_ but no double ..''--__
alphanumeric: same as alpha but with numbers
alphanumericspace: same as alphaspace but with numbers
date: dd-mm-yyyy
years: numeric maxiumum 2 numbers no spaces

i have been through 100 sites today only to find error after error

i found this one for email seems to work

$emailx ="/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$/";
shadiadiph
Junior Poster
119 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Added Bytes has a great collection of cheat sheets. The one in that link is for their regular expressions cheet sheet. It's been a lifesaver for me.

I also use Regular Expression Tester for testing my regex's before deployment.

david.1982
Light Poster
28 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

thanks david i will have a look

shadiadiph
Junior Poster
119 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Also if you want to learn the regex syntax the good old way then you can find the official documentation of the syntax at http://au2.php.net/manual/en/regexp.reference.php
You will find that the easiest way to learn is by starting with simple regex commands such as modifying existing ones then working you way up the scale untill you can memorize what everything does.

Also as for the regex validation you have requestion, the following may do the job:

<?
//name
if (pregmatch('/((*[ ]*|*\-\-*)?[ ](*[ ]*|*\-\-*)|(*[ ]*|*\-\-*)[ ](*[ ]*|*\-\-*)?)/i',$data)) {
//error found as name contains double dash or more than one space
}

//phone
if (pregmatch('/[^0-9]/i',$data)) {
//error found as it contains non-number characters
}
?>

---------
I thought I would share a joke from phpfreaks forums:
If preg was a women I would the pattern and she would replace me with $1.
Thats preg_replace right.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

i will give it a try thanks

shadiadiph
Junior Poster
119 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You