How to check whether a value is a number in JavaScript or jQuery

Possible Duplicate:
Validate numbers in JavaScript - 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

链接地址: http://www.djcxy.com/p/26398.html

上一篇: Javascript:验证一个字段:不为空&只包含数字

下一篇: 如何检查一个值是否是JavaScript或jQuery中的数字