1

I tried to make A javascript function to validate integer value at textbox. But I could not find any best way to validate it.

(Integer and Float value is acceptable but alphabets are not allowed).

1
  • 2
    Your question contains primary the must word and I didn't see anywhere the here's the code that I have tried so far ... words. Posting your teacher/client/manager requirements without illustrating what have you tried so far is not very appropriate here. You might need to hire a javascript developer. Commented Jul 28, 2011 at 11:17

2 Answers 2

2

I am not sure if I fully understand the question. Are you just checking to make sure that there are only integers typed into the textbox? If so, I would use a reg expression to check against the textbox. something like:

function isNum(elem, msg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
    return true;
}
     else{
    alert(msg);
    elem.focus();
    return false;
}

}

Sign up to request clarification or add additional context in comments.

Comments

1

Well in ultramodern browsers (and for today it means only webkit if i'm not mistaken) there as so-called html5 inputs. There is also an input with type="number". You can check whether it is supported or not this way:

function hasInputNumber() {
   var el = document.createElement("input");
   el.setAttribute("type", "number");
   var testval1 = "12121.121";
   var testval2= "1212.asas.a";
   el.value = testval1;
   var test1  = el.value === testval1;
   el.value = testval2;
   var test2 = el.value != testval2;
   return test1 && test2;
}

As for the rest, you can use input type="number" and try to mimic it's behaviour with javascript when this feature is not supported. People often try to do this listening keyboard events and cancelling any input except numeric. That is actually very difficult to implement fully and correctly, since we can paste input, validating float numbers on the fly is not so trivial and so one. So I advise you to validate only before submit.

As for the validation itself, there are 2 functions you can use - parseInt and parseFloat. Bu be carefull, there are some subtle issues. Always pass 10 as second param to parseInt and keep in mind that js will try to parse any data. For instance this - "121212sdsds" would not be considered as number in html5's input[type="number"]. But will be parsed as 121212 with parseFloat.

Last thing I should mention is NaN. NaN is a special type of number (yes, it is actually a number, since typeof NaN == "number") which does not equal to anything, even to itself. Both parseInt and parseFloat return NaN when fail to parse input. Since NaN != Nan the right way to check something is NaN is to call function isNaN.

I guess it's all i can tell about number validation in js :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.