Javascript isNumber / isNumeric function

 
Date Added24/09/2009
Version1.1
LanguageJavascript
CategoryForm validation
Rating
   8 (3 votes)
DescriptionThis function checks whether an inputted value is a number or not. There are 3 parameters:

the first is the string to check, - accepts a string value - this parameter is mandatory

the second optional parameter tells the function whether to include non whole numbers, accepts either true or false (true means that decimal points are accepted

the third optional parameter tells the function whether to include negative numbers, accepts true or false (true means that negative numbers are valid

[note, this script was updated on 16/11/09 to correct the case error in the last line of code.
InstructionsExample call to the function as follows:

alert(isNumeric('12345')) // - returns true

alert(isNumeric('-12345')) // returns false

alert(isNumeric('-12345',true,true)) // returns true

alert(isNumeric('12E45')) // returns false

alert(isNumeric('0.12345',false)) // returns false



alert(isNumeric('0.12345',true)) // returns true



Code



Comments
  1. Myke Black
    Myke Black 08 04 2015 4:29 PM

    This code only evaluates basic numbers, and does not work for the following cases: 8xe 0xff 99,999 If you need to be more flexible in which numbers can be evaluated, then use the jQuery.isNumeric() method instead, or use the following function: function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }

  2. Myke Black
    Myke Black 08 04 2015 4:31 PM

    This code only evaluates basic numbers, and does not work for the following cases: 8xe 0xff 99,999 If you need to be more flexible in which numbers can be evaluated, then use the jQuery.isNumeric() method instead, or use the following function: function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }

Leave your comment/rating
 

Related Scripts:

Javascript email validation - simple version

Javascript check valid phone number

Javascript check for valid URL syntax

Javascript improved email validation script



Tags



Back to main scripts page