Click to See Complete Forum and Search --> : modify reg exp


Webskater
12-15-2005, 09:00 AM
I am ashamed to admit I have absolutely no idea how this bit of code:

if (!/^\w+$/.test(userInput) || (/\_/g.test(userInput)))

tests for making sure a text string only contains a-z or 0-9 ...
but it seems to work.

Can anyone tell me what needs to be done to allow it to include an underscore, stop and a hyphen please.

Thanks very much.

Ultimater
12-15-2005, 09:08 AM
\w means a character 0-9 a-z A-Z or an underscore
It can also be written as [0-9a-zA-Z_]
If you wanted to include a hyphen, it would look like this:

if ( !/^[0-9a-zA-Z_-]+$/.test(userInput) )


Then there is always the shorthand:

if ( !/^[\w-]+$/.test(userInput) )


What do you mean by a "stop"? If you mean a period:

if ( !/^[\w\.-]+$/.test(userInput) )

Webskater
12-15-2005, 03:01 PM
Thanks for that.