View Full Version : regex for this ??

06-06-2007, 05:52 AM
Hello forums!!
I would like to know the regex for the following:
input text can contain
alphanumeric characters along with underscore and space
for example: "this is user_name"
i used the following:

/^[a-zA-Z0-9 _]+$/
this works but not perfectly because when the only space bar is pressed it takes the value, i want to prevent this, space should be @ the beginning, what will be the regex for this ?
Thanks in advance to all of you

06-06-2007, 05:56 AM
Give this a shot:

if ( preg_match( '/[\w\s]+/', trim( $var ) ) )
echo 'good';
echo 'bad';

mr e
06-06-2007, 06:02 AM
If you want to prevent only spaces from being entered, run it through trim() first to remove all spaces before and after the string