0

Hi mate,

I use regex for my validation but @ this time I get confuse, how to use white space in it.

I want user to enter a city name, some city name contains white space like Los Angeles, New York, I have regex which is not allowing user to enter digits with city name.

like,

if(ereg('[^A-Za-z]', $city))  {     
        echo "Only Characters Allowed";
    }
    else {
        echo "Thank You";
    }

This code does not allow user to enter digits in between city name, but this code does not allow me to enter space in between. If you have any suggestion for me then please draw my attention towards that.

Cheers!!

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by digital-ether
0

I think you just need to add the space within the square brackets, like so:

if(ereg('[^A-Za-z ]', $city))  {     
        echo "Only Characters Allowed";
    }
    else {
        echo "Thank You";
    }
0
if(ereg('[^A-Za-z1-9 ]', $city))  {     
        echo "Only Characters Allowed";
    }
    else {
        echo "Thank You";
    }

The REGEX engine allows a range of numbers, just like letters, in a white-list include, along with whitespace as you would just type it.

0

Just a note, you should avoid using ereg() as it is deprecated.

It is sad that it is, but it is. The PHP dev decided on using PECRE instead of POSIX Extended, those starting with "e". use the preg_ functions instead. ie: preg_match()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.