0

i have a function to validate (accept only integer and float)

function isNumeric(prodFilterValue)
{
    if((parseFloat(prodFilterValue) == parseInt(prodFilterValue)) && !isNaN(prodFilterValue))
    {
        alert('numeric');       
        alert('hahahahhahaha');
        $("#valuetxt1").css('background-color', 'white');           

    }
    else
    {
        alert('HIIIIIIIIIII');
        $("#valuetxt1").css('background-color', 'grey');
    }

    return prodFilterValue;
}

the problem now is.. when validate 0.1 it will say it is not numeric.. suppose to be it is numeric.. when i put another condition

if((parseFloat(prodFilterValue) == parseInt(prodFilterValue)) && !isNaN(prodFilterValue) || (prodFilterValue % 1 !=0))

it will validate 1A as numeric

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
ct_anas
  • 21
  • 1
  • Try this method used in Backbone http://stackoverflow.com/questions/9188998/obj-length-obj-length-in-javascript – Tamil May 04 '12 at 09:31

3 Answers3

2

Check it using Number conversion:

 !isNaN(Number([value]));
 // e.g.
 isNumber = !isNaN(Number('.3421')); //=> true
 isNumber = !isNaN(Number('nonumber.3421')); //=> false
 isNumber = !isNaN(Number('1500032')); //=> true
 // you can also just use the conversion 
 isNumber = Number('.3421'); //=> 0.3421
 isNumber = Number('nonumer.3421'); //=> NaN
 if (!isNumber) { /*...*/ }
 // a hidden goodie: Number automatically trims the parameter
 isNumber = Number('    .3421 ');   //=> 0.3421
 isNumber = Number('    .3421 \n'); //=> 0.3421

Applied to your function:

function isNumeric(prodFilterValue,thousandsDelim) {
  prodFilterValue = Number(prodFilterValue);
  return prodFilterValue 
            ? (alert('yeah!'), prodFilterValue) 
            : (alert('no sir'), false);
}

Furthermore isNaN([value]) applies an implicit Number conversion for [value], so you can use that too.

Keep in mind that Number(''), Number(null) or Number('\t\n ') all evaluate to 0 and Number(true) evaluates to 1. So, to be complete you'll have to do extra checks. Something like:

function isNumeric(prodFilterValue) {
    prodFilterValue = prodFilterValue && 
                      /stri/i.test(typeof prodFilterValue) && 
                      prodFilterValue.replace(/^\s+|\s+$/,'').length 
                       ? Number(prodFilterValue) 
                       : undefined;
    return prodFilterValue && prodFilterValue !== 0
            ? (alert('yeah!'), prodFilterValue) 
            : (alert('no sir'), false);
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

You can check if it is a number then would be float or integer.

function checkNumber(numb1)
{
  numb1 += "";   // to handle boolean when true / false is passed
  if(numb1.length == 0) return false; // to handle empty string like checkNumber("")

  if(isNaN(numb1))
      alert("It is a number");
  else
      alert("It is not a number");
}
Adil
  • 146,340
  • 25
  • 209
  • 204
0

That's because parseInt and parseFloat will throw away any letters after a number, and parse 4K as 4. Add an additional condition: isFinite(prodFilterValue) and that should handle those cases.

parseInt and parseFloat will only return NaN if the first character cannot be converted to a number.

Be aware though, parseInt also handles values like hexadecimal:

parseInt("0xF"); // 15
Joakim Johansson
  • 3,196
  • 1
  • 27
  • 43