1

So, I would like to limit an input field to only allow numbers and hyphens. I'm not interested in changing my methodology, but would like to understand why my regex matches numbers but not hyphens.

Regex:

/[^0-9-]/g

jsFiddle: http://jsfiddle.net/abriggs/7G6JD/

j08691
  • 204,283
  • 31
  • 260
  • 272
abriggs
  • 744
  • 7
  • 16

3 Answers3

2

Adding simple console lines will show the error

function numbersOnly(number, allowDash) {
    console.log("In numbersOnly);
    // Filter non-digits/dash from input value.
    if (allowDash) { 
        console.log("1:", number);
        number = number.replace(/[^0-9\-]/g, '');
        console.log(2:", number);
    } else {
        // Filter non-digits from input value.
        number = number.replace(/\D/, '');
    }
    return number;
}

Type in 1

In numbersOnly
1: 1
2: 1

Type in -

In numbersOnly
1: ½ 
2:

So your problem is with the line String.fromCharCode(e.which)

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks, your'e right. +1, but accepted answer from Sujith PS because he provided a complete solution. – abriggs Jan 17 '14 at 14:41
2

Problem was with keycode of - ,

keycode of - is 189 or 109 .

But String.fromCharCode(189) is ½

String.fromCharCode(109) is m

You can use the following code :

function numbersOnly(number, allowDash) {
    // Filter non-digits/dash from input value.
    console.log(number);
    if (allowDash) {
        number = number.replace(/[^0-9\-]/g, '');
    } else {
        // Filter non-digits from input value.
        number = number.replace(/\D/, '');
    }
    return number;
}

$(function(){

    // Do not allow non-numeric characters in bill zip code
    $('#billZip').keydown(function(e) {
        console.log(e.keyCode);
        if (e.keyCode != 8 && e.keyCode != 37 && e.keyCode != 39) {
              if(e.keyCode ===189||e.keyCode ===109)
                 if (numbersOnly(String.fromCharCode(45), true) != "")
                    return true;
                 else 
                     return false

            if (numbersOnly(String.fromCharCode(e.which), true) != "")
                return true;
            else return false
        }
    });

});

Fiddle

Community
  • 1
  • 1
Sujith PS
  • 4,776
  • 3
  • 34
  • 61
0

The only way to do this is to have the hyphen as the FIRST or LAST character within the character class such as this:

[-0-9] or [0-9-]

However, you are using a "^" which negates everything in the character class, so if you only want hyphens and numbers you should not have the caret.

Source: http://www.regular-expressions.info/charclass.html

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • 1
    Fixed to add both options. – JNYRanger Jan 17 '14 at 14:31
  • Something might be wrong with your code, but the regex itself tests out. – JNYRanger Jan 17 '14 at 14:43
  • The regex might be fine alone, but if it doesn't solve the OP's problem then it's not really a valid answer. Your solution shouldn't just point out part of the problem, it should fully answer all parts of the question and factor in the full code provided. – j08691 Jan 17 '14 at 14:52
  • There's already an accepted answer so no need at this point. The fiddle shows that they needed to add some more keycodes. – JNYRanger Jan 17 '14 at 14:56