regex for this ??

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

regex for this ??

Post by PHPycho »

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:

Code: Select all

/^[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 [/quote]
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

I found this one .......

Code: Select all

/^[a-z0-9_][a-z0-9_ ]*$/i
any other alternatives
thanks a lot
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

/^[a-z0-9 _]+$/iD
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

And.. You'll probably want to give some sort of length specification.

Code: Select all

/^[a-z0-9_]{6,18}$/i
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Descriptive subjects

Post by feyd »

Please update the title PHPycho.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
Post Reply