The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Allow only alphanumeric characters, white spaces and hyphen.
Discuss Allow only alphanumeric characters, white spaces and hyphen. in the Regex Programming forum on Dev Shed. Allow only alphanumeric characters, white spaces and hyphen. Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 27th, 2009, 05:47 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
Allow only alphanumeric characters, white spaces and hyphen.
Hello,
I have come up with this regex for making sure that given string contains only alphanumeric characters or spaces or a hyphen.
php Code:
Original
- php Code |
|
|
|
$val = $argv[1]; $reg = "#[^a-z0-9\s-]#i"; if ($count > 0){ echo "$val contains invalid characters\n"; } else{ echo "$val DOES NOT contain invalid characters\n"; }
I have tested and it seems okay. I am not well versed with regex, so came here for a confirmation.
If you didn't already know, it is in PHP PCRE.
Thank you.
__________________
Why do we always seek someone, something or some thought? Are we afraid of ourselves?
|

January 27th, 2009, 04:55 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Looks good.
Remember that "\s" includes \n(ewline) \r(eturn) \t(tab) \v(ertical tab) \f(orm feed) and space.
|

January 28th, 2009, 04:59 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
Thank you. Is there an expression to allow only space and deny newlines, tabs etc?
|

January 28th, 2009, 03:44 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Besides the "expression" you get when you press the spacebar on your keyboard?
|

January 29th, 2009, 06:07 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
I'm not sure i followed you. I will try to make myself clearer. In terms of keyboard, only alphabets, numbers, hypen and space bar are to be allowed.
|

January 29th, 2009, 04:22 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Since \s included any kind of whitespace, your question was what can be used to only represent a space.
And I'm telling you, a space will represent a space.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|