[Solved-6 Solutions] Validate decimal numbers - javascript tutorial



Problem:

How to validate decimal numbers in JavaScript ?

Solution 1:

match() method retrieves the matches when matching a string against a regular expression

To Validate Decimal numbers, we can use the following code:

<html>
   <body>
      <script>
         var str = "1.5";
         var wikitechy = /^[-+]?[0-9]+\.[0-9]+$/;
         var found = str.match( wikitechy );
         document.write("decimal" );
      </script>
   </body>
</html>

Solution 2:

/ Whitespace strings:
IsNumeric(' ') == true;
IsNumeric('\t\t') == true;
IsNumeric('\n\r') == true;

// Number literals:
IsNumeric(-1) == false;
IsNumeric(0) == false;
IsNumeric(1.1) == false;
IsNumeric(8e5) == false;

To implement an IsNumeric function, to find out if a variable contains a numeric value, regardless of its type, it can be a String containing a numeric, a Number object, virtually anything can be passed to that function.
We couldn't make any type assumptions, taking care of type coercion (eg. +true == 1; but true shouldn't be considered as "numeric").

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

Solution 3:

  • RegEx is easy to make subtle, impossible to spot mistakes with your regular expression.
  • If we can't use isNaN(), this should work fine:
function IsNumeric(input)
{
    return (input - 0) == input && (''+input).trim().length > 0;
}
  • The (input - 0) expression forces JavaScript to do type coercion on your input value, it must first be taken as a number for the subtraction operation.
  • If that conversion to a number fails, the expression will result in NaN. This numeric result is then compared to the original value you passed in.
  • Since the left-hand side is numeric, type coercion is again used. Now that the input from both sides was coerced to the same type from the same original value, you would think they should always be the same (always true).
  • However, NaN is never equal to NaN, so that value can't be converted to a number (and only values that cannot be converted to numbers) will result in false.
  • The check on the length is for a special case involving empty strings.
  • Note that it falls down on your 0x89f test, but that's in many environments that's an easy way to define a number literal. If we need to catch that specific scenario we can add an additional check.

Solution 4:

This solution works fine

function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}
  • And to test it:
// alert(TestIsNumeric());

function TestIsNumeric(){
    var results = ''
    results += (IsNumeric('-1')?"Pass":"Fail") + ": IsNumeric('-1') => true\n";
    results += (IsNumeric('-1.5')?"Pass":"Fail") + ": IsNumeric('-1.5') => true\n";
    results += (IsNumeric('0')?"Pass":"Fail") + ": IsNumeric('0') => true\n";
    results += (IsNumeric('0.42')?"Pass":"Fail") + ": IsNumeric('0.42') => true\n";
    results += (IsNumeric('.42')?"Pass":"Fail") + ": IsNumeric('.42') => true\n";
    results += (!IsNumeric('99,999')?"Pass":"Fail") + ": IsNumeric('99,999') => false\n";
    results += (!IsNumeric('0x89f')?"Pass":"Fail") + ": IsNumeric('0x89f') => false\n";
    results += (!IsNumeric('#abcdef')?"Pass":"Fail") + ": IsNumeric('#abcdef') => false\n";
    results += (!IsNumeric('1.2.3')?"Pass":"Fail") + ": IsNumeric('1.2.3') => false\n";
    results += (!IsNumeric('')?"Pass":"Fail") + ": IsNumeric('') => false\n";
    results += (!IsNumeric('blah')?"Pass":"Fail") + ": IsNumeric('blah') => false\n";

    return results;
}

Try this Regex:

/^ match beginning of string
-{0,1} optional negative sign
\d* optional digits
\.{0,1} optional decimal point
\d+ at least one digit
$/ match end of string

Solution 5:

function IsNumeric(num) {
     return (num >=0 || num < 0);
}

This works for 0x23 type numbers.

Solution 6:

Use this code:

isNumber: function(o) {
    return typeof o === 'number' && isFinite(o);
}


Related Searches to Validate decimal numbers - javascript tutorial