开发者

Check whether a value is a number in JavaScript or jQuery [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Validate numbers in JavaScrip开发者_StackOverflow社区t - IsNumeric()

var miscCharge = $("#miscCharge").val();

I want to check misCharge is number or not. Is there any method or easy way in jQuery or JavaScript to do this?

HTMl is

<g:textField name="miscCharge"  id ="miscCharge" value="" size="9" max="100000000000" min="0" />


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


You've an number of options, depending on how you want to play it:

isNaN(val)

Returns true if val is not a number, false if it is. In your case, this is probably what you need.

isFinite(val)

Returns true if val, when cast to a String, is a number and it is not equal to +/- Infinity

/^\d+$/.test(val)

Returns true if val, when cast to a String, has only digits (probably not what you need).


there is a function called isNaN it return true if it's (Not-a-number) , so u can check for a number this way

if(!isNaN(miscCharge))
{
   //do some thing if it's a number
}else{
   //do some thing if it's NOT a number
}

hope it works

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜