...

View Full Version : Validating the character: ?



nightkarnation
07-19-2012, 04:09 AM
Hey guys!
I have one missing part on my code...

Here it is:



var srch = "?Test_1234-";
var valid = /^[A-Za-z0-9_-]{9,25}$/;
if ( valid.test( srch ) )
{
//etc...
}


I need to validate srch if it contains only a-zA-Z0-9 _-?...min 9 and max 25 of length
The only problem I have is with the ? (question sign) character. I don't know how and where to place it in the valid variable...
Any ideas?
Thanks a lot in advance!!

MarPlo
07-19-2012, 07:18 AM
Hi
Add the \ character before special characters in regexp pattern.
In your code:

var valid = /^[A-Za-z0-9\?_-]{9,25}$/;

Old Pedant
07-19-2012, 11:59 PM
And as a minor improvement: \w is shorthand for [A-Za-z0-9_]

SO you could use

var valid = /^[\w\?\-]{9,25}$/;

Philip M
07-20-2012, 08:07 AM
For the record, the META characters which must be escaped in a pattern are: ^ $ \ / ( ) | ? + * [ ] { } . (but not , )
Strictly the hyphen - does not need to be escaped in a pattern unless it is in a character class [] - and not then if it is the last character before the ].



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum