4

Is there any simple method in JavaScript / jQuery to check whether the variable is a number or not (preferably without a plugin)? I want to alert whether the variable is a number or not.

Thanks in advance...:)

3
  • stackoverflow.com/questions/995183/… Commented Apr 14, 2011 at 12:31
  • no.. This is not the repeat of the question mentioned. I am looking to validate without a plugin.. Commented Apr 14, 2011 at 13:34
  • 1
    do accpet ans if it work for you Commented Apr 21, 2011 at 7:12

5 Answers 5

19

I wouldn't recommend the isNaN function to detect numbers, because of the Java Script type coercion.

Ex:

isNaN(""); // returns false (is number), a empty string == 0
isNaN(true); // returns false (is number), boolean true == 1
isNaN(false); // returns false (is number), boolean false == zero
isNaN(new Date); // returns false (is number)
isNaN(null); // returns false (is number), null == 0 !!

You should also bear in mind that isNaN will return false (is number) for floating point numbers.

isNaN('1e1'); // is number
isNaN('1e-1'); // is number

I would recommend to use this function instead:

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
Sign up to request clarification or add additional context in comments.

6 Comments

so what are yo trying to say, do i modify @Pranay Rana's answer??
I was just trying to say that using isNaN function is not the most "correct" way to check if a string is a number in Java Script.
is there any other simple way?? can u make me a fiddle?? :)
You don't like the function I have sugested to use in the answer?
I like.. Can u demonstrate it with a fiddle??
|
4

Checking number using isNaN function

var my_string="This is a string";
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}

or

Check whether a number is an illegal number:

<script type="text/javascript">


    document.write(isNaN(5-2)+ "<br />");
    document.write(isNaN(0)+ "<br />");
    document.write(isNaN("Hello")+ "<br />");
    document.write(isNaN("2005/12/12")+ "<br />");

</script>

The output of the code above will be:

false
false
true
true 

1 Comment

hello @Pranay Rana, can u demonstrate it in a fiddle??
1

Can use below code for this. I would not rely on isNaN() completely. isNaN has shown inconsistent results to me (e.g - isNaN will not detect blank spaces.).

//Event of data being keyed in to textbox with class="numericField".
$(".numericField").keyup(function() {
    // Get the non Numeric char that was enetered
    var nonNumericChars = $(this).val().replace(/[0-9]/g, '');                                  
    if(nonNumericChars.length > 0)
        alert("Non Numeric Data entered");
});

4 Comments

Good point, but what happens when the user deletes all the characters in the input?
nonNumericChars.length will still be zero and the alert will not be thrown. Are you looking for anything else to do with tat event?
Ah, you're right. I thought I had caught you with an error, but it turns out I was wrong. I like your answer.
Thats ok. Best part is we can always some way to fix all errors. so if you can find errors, its better I will learn more :)
0
function isDigit(num) {
    if (num.length>1){return false;}
    var string="1234567890";
    if (string.indexOf(num)!=-1){return true;}
    return false;
}

You need to loop through the string and call this function for every character

Comments

0

use standard javascript functions

isNaN('9')// this will return false
isNaN('a')// this will return true

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.