【问题标题】:Jquery check if value is numericJquery检查值是否为数字
【发布时间】:2013-01-06 00:24:54
【问题描述】:

我想知道是否有人有一个快速而肮脏的 jquery 方法来检查一个值是否为数字?我正在考虑使用正则表达式类型方法来检查值,如果没有,请不要提交表单。我正在使用 jquery 验证,但我在加载 jquery 验证时遇到了问题。

我只有一个值,我想确保它是数字。

【问题讨论】:

  • 你不能做一个简单的解析,因为它有时会从非数字字符串中返回一个数字 - 试试console.log(parseInt("10px", 10))

标签: jquery numeric


【解决方案1】:

当然,使用 jQuery 的 isNumeric() 函数。

$.isNumeric("-10");  // true
$.isNumeric(16);     // true
$.isNumeric(0xFF);   // true
$.isNumeric("0xFF"); // true
$.isNumeric("8e5");  // true (exponential notation string)
$.isNumeric(3.1415); // true
$.isNumeric(+10);    // true
$.isNumeric(0144);   // true (octal integer literal)
$.isNumeric("");     // false
$.isNumeric({});     // false (empty object)
$.isNumeric(NaN);    // false
$.isNumeric(null);   // false
$.isNumeric(true);   // false
$.isNumeric(Infinity); // false
$.isNumeric(undefined); // false

【讨论】:

  • 有时它会给出 TypeError: $.isNumeric is not a function then use jquery.isNumeric() function
  • 也得到一个 TypeError
  • +1 因为您在 cmets 中写入了与 jquery 网站文档相比每次调用的结果。
  • 注:$.isNumeric("1,234"); // false
  • 注意:此 API 在 jQuery 3.3 中已被弃用。
猜你喜欢
  • 2011-06-28
  • 2013-06-17
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多