0

i need a javascript function that able to check for digit and - only.

example: 1,2,3,4,5,6,7,8,9,0 will return true and - will return true as well.

other than that all return false including enter is pressed.

i have a function like this:

function IsNumeric(sText){
    var filter = /^[0-9-+]+$/;
    if (filter.test(sText)) {
        return true;
    }else {
        return false;
    }
}

i call it like this:

if(!IsNumeric(value)) {
  alert("Number and - only please");
}

for some reason it does not work, any method to do the verification without using regex?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Redbox
  • 1,457
  • 5
  • 17
  • 22
  • Possible duplicate of http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric – Florian Margaine Jun 19 '12 at 06:36
  • Your regex is working fine. Can you specify a test case for which it fails? You mean 123-211 should return false? – Tamil Jun 19 '12 at 06:36
  • The function works fine. But I see that you are considering keypress, as you say enter will return false. Can you add the part where you are getting the value from? – Nandini Bhaduri Jun 19 '12 at 06:42

4 Answers4

2

EDIT: OK, updated as per your comment, an expression to match either a lone minus sign or any combination of digits with no minus sign:

function IsNumeric(sText){
   return /^(-|\d+)$/.test(sText);
}

If you want only positive numbers and don't want to allow leading zeros then use this regex:

/^(-|[1-9]\d*)$/

Regarding your question "any method to do the verification without using regex?", yes, there are endless ways to achieve this with the various string and number manipulation functions provided by JS. But a regex is simplest.

Your function returns true if the supplied value contains any combination of digits and the plus or minus symbols, including repeats such as in "---+++123". Note that the + towards the end of your regex means to match the preceding character 1 or more times.

What you probably want is a regex that allows a single plus or minus symbol at the beginning, followed by any combination of digits:

function IsNumeric(sText){
   return /^[-+]?\d+$/.test(sText);
}

? means match the preceding character 0 or 1 times. You can simplify [0-9] as \d. Note that you don't need the if statement: just return the result from .test() directly.

That will accept "-123", "123", "+123" but not "--123". If you don't want to allow a plus sign at the beginning change the regex to /^-?\d+$/.

"example: 1,2,3,4,5,6,7,8,9,0 will return true and - will return true as well."

Your example seems to be saying that only a single digit or a single minus sign is considered valid - if so then try this:

function IsNumeric(sText){
   return /^[\d-]$/.test(sText);
}

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

How about

function IsNumeric(s) {
    return /^(+|-|)\d*$/.test(s);
}
Ephemera
  • 162
  • 1
  • 6
1

Hiphen(-) has special meaning so use escape character in character set.

Try this:

       var filter = /^[0-9\-]+$/;
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44
0

Can be simple ... try this:

function IsNumeric(str) {
    return str.length == 1 && (parseInt(str) < 10 || str == "-");
}
McGarnagle
  • 101,349
  • 31
  • 229
  • 260