Regular Expression To Restrict Special Characters
i have an address field in my form and i want to restrict * | \ ' : < > [ ] { } \ ( ) '' ; @ & $ i have tried with var nospecial=/^[^* | \ ' : < > [ ] { } ` \ (
Solution 1:
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 */ }
Solution 2:
/[$&+,:;=?[]@#|{}'<>.^*()%!-/]/
below one shouldn't allow to enter these character and it will return blank space
.replace(/[$&+,:;=?[\]@#|{}'<>.^*()%!-/]/,"");
Solution 3:
Use the below function
function checkSpcialChar(event){
if(!((event.keyCode >= 65) && (event.keyCode <= 90) || (event.keyCode >= 97) && (event.keyCode <= 122) || (event.keyCode >= 48) && (event.keyCode <= 57))){
event.returnValue = false;
return;
}
event.returnValue = true;
}
Solution 4:
use this this will fix the issue
String patttern = r"[!-/:-@[-`{-~]";
RegExp regExp = RegExp(patttern);
Post a Comment for "Regular Expression To Restrict Special Characters"