Forum Moderators: coopster

Message Too Old, No Replies

regex for this?

         

PHPycho

3:52 am on Jun 6, 2007 (gmt 0)

10+ Year Member



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:
[php]/^[a-zA-Z0-9]+$/[/php]
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

eelixduppy

4:00 am on Jun 6, 2007 (gmt 0)



Something like this?:

$pattern = "/^\s?[a-zA-Z0-9]+$/";

Refer to pattern syntax [us.php.net] for more information. good luck

PHPycho

4:15 am on Jun 6, 2007 (gmt 0)

10+ Year Member



sorry this was my used code
/^[a-zA-Z0-9 _]+$/
how to modify above to remove if only spaces are entered
Note: space can come after alphanumerics
for ex: "this is"

whoisgregg

1:21 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps trim() [php.net] or ltrim() [php.net] (as appropriate) before running the regex would help?