i have an address field in my form and i want to restrict * | \ " : < > [ ] { } \ ( ) '' ; @ & $
i have tried with
var nospecial=/^[^* | \ " : < > [ ] { } ` \ ( ) '' ; @ & $]+$/;
if(address.match(nospecial)){
alert('Special characters like * | \ " : < > [ ] { } ` \ ( ) \'\' ; @ & $ are not allowed');
return false;
but it is not working. Please tell me what i missed?
You need to escape the closing bracket (as well as the backslash) inside your character class. You also don't need all the spaces:
var nospecial=/^[^*|\":<>[\]{}`\\()';@&$]+$/;
I got rid of all your spaces; if you want to restrict the space character as well, add one space back in.
EDIT As @fab points out in a comment, it would be more efficient to reverse the sense of the regex:
var specials=/[*|\":<>[\]{}`\\()';@&$]/;
and test for the presence of a special character (rather than the absence of one):
if (specials.test(address)) { /* bad address */ }
/[$&+,:;=?[]@#|{}'<>.^*()%!-/]/
below one shouldn't allow to enter these character and it will return blank space
.replace(/[$&+,:;=?[\]@#|{}'<>.^*()%!-/]/,"");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With