-2

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

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • 3
    Why would you do this when a whitelist is clearly much easier to implement – Hubro Apr 23 '12 at 02:09
  • I also agree that whitelist is simplier than blacklist in most scenarios. Also similar qusetion here http://stackoverflow.com/questions/756567/regular-expression-for-excluding-special-characters – Faris M Apr 23 '12 at 02:11
  • 2
    What should happen if someone enters `µ` or `ß` or any of the thousands of other non-ASCII characters? People are recommending a white list for a reason. – mu is too short Apr 23 '12 at 02:13
  • what if someone uses characters from other languages? Chinese and Japanese have a lot of symbols. – Joseph Apr 23 '12 at 02:19
  • its ok for me to accept any other languages like 日本語(japanese) – Netorica Apr 23 '12 at 02:53
  • @Joseph as i said. I want a blacklist ^^ of the character ranges i wanted to block – Netorica Apr 23 '12 at 02:57
  • thank you for the downvotes. but as i said.. i don't need a whitelist... but a blacklist – Netorica Apr 23 '12 at 03:54

2 Answers2

3

Though I'm with the others and recommend a whitelist, here's how you would do a blacklist using a regex:

// 0-47 : 00-2F : control codes, spaces, punctuation
// 58-64 : 3A-40 : more punctuation
// 91-96 : 5B-60
// 123-127 : 7B-7F

function verifyChars(str) {
    return str.match(/^[^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+$/) != null;
}

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()):

var verifyChars = (function() {
    var re = /^[^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+$/;
    return function(str) {
        return re.test(str);
    }
})();

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

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

More of a comment, others have pretty much covered why testing for allowed characters is, in this case, likely much simpler than testing for excluded characters:

> if($("#txtTag").val().length <=0 || $("#txtTag").val() =="") {

Presumably txtTag is the ID of an element like an input or textarea. In that case, the value is a string, its length can never be less than zero. Also, the length of the empty string "" is zero, so it is covered by the first test, so the above can be simply:

if ($("#txtTag").val().length == 0) 

Then you have:

> else if(!RegexCheck(/^[a-zA-Z\_]+$/g,$("#txtTag").val())){   

Your code would be more efficient as:

var re = /^[a-zA-Z\_]+$/;
var value = $("#txtTag").val(); // or document.getElementById('txtTag').value

if (value == '') {
  // value is blank, do something

) else if (!re.test(value)) {
  // value has characters outside the restricted set
}

Incidentally, if you set the default value of the element to "Please enter keyword", you can do:

var el = document.getElementById('txtTag');

if (el.value == '') {
  // nothing has been entered
  el.value = el.defaultValue; // reset to 'Please enter keyword"

} else if (...) {
RobG
  • 142,382
  • 31
  • 172
  • 209