Page 1 of 1

How to modify a preg_match function to accept spaces

Posted: Sun Sep 20, 2009 4:13 pm
by edawson003
How can I modify the below preg_match function to accept [letters][space][letters]. Want to still exclude numbers and other characters.

If it is easy, wouldn't mind excluding these entry options as well:
-[space][letters]
-[letters][space]
But, no big deal as I can trim those out when I post form.

Code: Select all

 
elseif(!preg_match("/^([a-zA-Z])+$/", $_POST['servingunits'])) {
     $reqverrors[] = "Yes";
     $errName = "\t<br><span class=pred>Units must be from letters and spaces.</span>\n";
     }
 

I tried this but it didn't work

Code: Select all

 
elseif(!preg_match("/^([a-zA-Z[color=#FF0000] ]) ([ a-zA-Z])[/color]+$/", $_POST['servingunits'])) {
     $reqverrors[] = "Yes";
     $errName = "\t<br><span class=pred>Units must be from letters and spaces.</span>\n";
     }
 

Re: How to modify a preg_match function to accept spaces

Posted: Sun Sep 20, 2009 9:54 pm
by Eric!
Try the \s instead for a whitespace. See character classes:

http://www.zytrax.com/tech/web/regex.htm

Re: How to modify a preg_match function to accept spaces

Posted: Mon Sep 21, 2009 1:30 am
by edawson003
Ok, trying to get it to work. Where do I place the \s?

:banghead:

I tried this:

Code: Select all

elseif(!preg_match("/^([a-zA-Z][color=#FF0000]\s[/color])+$/", $_POST['servingunits'])) {
I tried this:

Code: Select all

elseif(!preg_match("/^([a-zA-Z][color=#FF0000]+\s[/color])+$/", $_POST['servingunits'])) {