As others recommended “create a whitelist” but I really wanted to create a blacklist.
this is my code
if($("#txtTag").val().length <=0 || $("#txtTag").val() =="")
{
$("#ep-insert-keyword").html("Please Enter Keyword");
$("#ep-insert-keyword").fadeIn("normal");
}
else if(!RegexCheck(/^[a-zA-Z\_]+$/g,$("#txtTag").val()))
{
$("#ep-insert-keyword").html("Special characters are not allowed");
$("#ep-insert-keyword").fadeIn("normal");
}
I want to prevent my users to enter any special character I defined in my regex.
the characters i want to prevent are this ranges of characters in ASCII
0-47 58-64 91-96 123-127
I really want to reverse my code from whitelist to BLACKLIST… but i don’t know how.
I don’t worry about other languages as i really want them to pass my validation… i just want to block the characters on the ASCII ranges i wanted
Though I’m with the others and recommend a whitelist, here’s how you would do a blacklist using a regex:
And here’s a jsFiddle with a bunch of test cases in it: http://jsfiddle.net/jfriend00/24xF7/
Or a little more efficient (because it only evaluates the regex once at startup and uses
.test()):jsFiddle of this one with test cases: http://jsfiddle.net/jfriend00/fZ3AN/
FYI, here’s a good reference on how to put unprintable chars into a regex: http://www.regular-expressions.info/characters.html