I am currently using some regex for validation on a text field in JSP.
However, special characters like \ " will make the JSP returns error.
Now I want to write a regex to disallow user input such special characters.
This is what I write
[a-zA-Z0-9,;:<>?/~!@#$%`()-_=+.^&]
however, user still able to input \
why is it so? I think my regex must be wrong, thanks for correction identifications!!!
THANK YOU ALL SO MUCH!!!
Regex help! Thanks so much!
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Pseudo since I don't do JSP:
Obviously you need to keep adding the characters you want to disallow. It may be easier to only "allow" certain characters. For example:
Notice that I escape lots of characters with a backslash because they already have a special meaning in regex. Also note the use of caret "^" in my first example at the start of the square brackets: [^ ]. The caret in those brackets negates the list of characters inside it.
Notice also the caret "^" and dollar "$" at the start and end of the entire pattern in both my examples. These make sure that the pattern matches the entire string from start to end.
Code: Select all
if ( string.match(/^[^\\%\$\"\'\^\*]+$/) )
{
//All OK
}Code: Select all
if ( string.match(/^[\w\s\.,;:\!\?&]+$/) )
{
//All OK
}Notice also the caret "^" and dollar "$" at the start and end of the entire pattern in both my examples. These make sure that the pattern matches the entire string from start to end.