繁体   English   中英

使用正则表达式在javascript / jquery代码中查找非数字值

[英]using regular expressions to find non-numeric values in javascript/jquery code

本质上,我试图查找用户在文本字段中输入的任何非数字值。 目前,我的代码如下所示:

    else if(MileInputElement.match([0-9]))
    {
        InvalidEntry= "Non-numeric values"; 
    }

但是,当我运行此程序时,出现错误提示

对象不支持属性或方法“匹配”。

那么,有什么更好的方法来解决呢?

您在这里: \\D为非数字, g为所有匹配项, match返回一个数组

 alert('a9bc8'.match(/\\D/g)); 

希望这可以帮助。

使用正则表达式定界符,并且还需要在应用match函数之前将对象转换为字符串。

else if(JSON.stringify(MileInputElement).match(/[^0-9]/))
    {
        InvalidEntry= "Non-numeric values"; 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM