SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Checking number using isNaN function


JavaScript has one important function isNaN() to check any data is number or string. This function isNaN() is remembered as short form is Is Not a Number. This function returns true if the data is string and returns false if the data is a number. Here is one simple example to check one string. We will use one if else condition check to display a message accordingly. Here is the code.

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 ");
}


We will integrate this function to one JavaScript prompt. Here we will ask the user to enter a number. The value entered by user we will check by isNan function and display message accordingly. Here is the code.

var my_string = prompt("Please enter a number","");
document.write(my_string)
if(isNaN(my_string)){
document.write ("this is not a number ");
}else{document.write ("this is a number ");
}


Further readings
random() function to generate random numbers
round() function to get rounded value of a number
IsNaN() function to check data if number or not
ceil() function to get just next higher integer
floor() function to get just highest lower integer
toFixed() function to format decimal places of a number
toPrecision() function to fix the places of a number
parseFloat() function to convert string to floating number
parseInt() function to convert string to number
modulus % to get the reminder of a division
Me207-03-2009
Cool
tcx11-10-2009
yes, agree,
this is very useful topic it helps me in project,
good concept not finding in other sites
Dhaval Shingala25-02-2010
The simplest Way is Why u check entered input is Number or not

just check on Keypress Event is number or not

The Function for checking Is number is

function isNumberKey(B,C){if(!C){C=0}var A=(B.which)?B.which:event.keyCode;if(A>31
Rakesh Joshi16-04-2010
I observed that isNaN has a flaw. If we enter a value with alphabets at the start, it will detect. But if it starts with a number and continues with alphabets or ends with alphabets, then isNaN will return false, meaning to say it is a number, which is not the desired output. Is there any other alternative?
Adamu sani yahaya01-06-2010
write a javascript code that will pop-up when an input of not a number is enter and return true when number data is enter. but without using in inbuild function such as isNan.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
Math Functions
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.