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}$/";

Recommended Answers

All 4 Replies

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.

thanks david i will have a look

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.

i will give it a try thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.