Skip to main content
edited tags
Link
nnnnnn
  • 150.4k
  • 30
  • 210
  • 247
Source Link
Redbox
  • 1.5k
  • 6
  • 17
  • 22

javascript number validation function

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?