Page 1 of 1
Help with Regex: Alphanumeric AND spaces
Posted: Sat Jul 21, 2007 1:44 pm
by matt1019
Hi Guys,
Loooooooong time, no post!
I have this if statement using regex, which works just fine, except, I cannot seem to make it accept spaces as well...
Code: Select all
if(!eregi("^([0-9a-z])*$", $address)){
.....
}
How can I modify the regex to let the user input spaces also? [Again, it works just fine with letters and numbers....]
Thanks!
-Matt
Posted: Sat Jul 21, 2007 1:50 pm
by feyd
....add a space.
switch to
preg_match() too.

Posted: Sat Jul 21, 2007 1:52 pm
by matt1019
Hi feyd!
Any particular reason why I should switch form eregi to preg_match? (just wanted to know the reason behind it...)
Thanks.
-Matt
Posted: Sat Jul 21, 2007 2:01 pm
by Chalks
Pretty sure ereg is being phased out of php5.
edit: here's one answer:
Mastering Regular Expressions wrote:However, many of the more modern regex features such as lazy quantifiers, lookaround and Unicode are not supported by the ereg functions.
from
here
Posted: Sat Jul 21, 2007 2:09 pm
by matt1019
Hi Chalks,
Thanks for the info. I will change my code... as soon as I get it's replacement (preg_match) to work correctly.
This is what I came up with:
Code: Select all
if(!preg_match("/[0-9A-Za-z\s\-]+/",$address)){
... SHOW ERROR ...
}
however.... this is a big problem... as it allows for tags (such as <html> etc. etc. etc.)
How can I fix that?
Thanks,
-Matt
Posted: Sat Jul 21, 2007 2:16 pm
by superdezign
It's not checking every character.
Code: Select all
if(!preg_match("/^[0-9A-Za-z\s\-]+$/",$address)){
... SHOW ERROR ...
}
Posted: Sat Jul 21, 2007 3:13 pm
by matt1019
Hi,
Got it. Just made a simple modification to allow for commas:
Code: Select all
if(!preg_match("/^[0-9A-Za-z\s\,-]+$/", $address)){
... SHOW ERROR ...
}
thanks once again!
-Matt
Posted: Sat Jul 21, 2007 3:39 pm
by superdezign
Why are you now escaping the comma, and not the dash?
Posted: Sat Jul 21, 2007 9:42 pm
by feyd
The dash doesn't require escaping.. nor does the comma.
Posted: Sun Jul 22, 2007 3:03 am
by stereofrog
matt1019 wrote:
Code: Select all
if(!preg_match("/^[0-9A-Za-z\s\,-]+$/", $address)){
... SHOW ERROR ...
}
This will validate, among others, a string consisting entirely of spaces. I'm sure that's not what you intended.