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!!

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";
    }
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.

Thanks buddy, it's worked, thanks for your time!!!

If you could mark the thread as solved, that would be great.
Thanks!

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()

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.