jQuery.isNumeric()


jQuery.isNumeric( value )返回: Boolean

描述: 确定它的参数是否是一个JavaScript数字。

  • 添加的版本: 1.7jQuery.isNumeric( value )

    • value
      类型: Anything
      用于测试的值。

$.isNumeric()方法检查它的参数是否能代表一个数值。如果是这样,则返回true。否则返回false。该参数可以是任何类型。(注:jQuery 3.0之前,这个方法会强制转换参数为Number,转换后的值类型如果是如果是Number,也会返回true

在jQuery 3.0中,$.isNumeric()方法只有接收number类型的参数时候,或者是可以被强制为有限数值的 string类型的参数,才会返回true,在其他情况下,返回false

例子:

以下是使用 $.isNumeric 检测各种输入类型的例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// true (numeric)
$.isNumeric( "-10" )
$.isNumeric( "0" )
$.isNumeric( 0xFF )
$.isNumeric( "0xFF" )
$.isNumeric( "8e5" )
$.isNumeric( "3.1415" )
$.isNumeric( +10 )
$.isNumeric( 0144 )
// false (non-numeric)
$.isNumeric( "-0x42" )
$.isNumeric( "7.2acdgs" )
$.isNumeric( "" )
$.isNumeric( {} )
$.isNumeric( NaN )
$.isNumeric( null )
$.isNumeric( true )
$.isNumeric( Infinity )
$.isNumeric( undefined )