简体   繁体   English

Javascript正则表达式可查找输入中除浮点数以外的任何字符

[英]Javascript regular expression to find any character other than floating point numbers in the input

The user can provide any kind of data. 用户可以提供任何种类的数据。 I need to make sure the input string contains only floating point number. 我需要确保输入字符串仅包含浮点数。 So I need to make sure the data contains only numbers or a dot (.) and return false if the input data has anything other than numbers or a dot. 因此,我需要确保数据仅包含数字或点(。),如果输入数据具有数字或点以外的其他内容,则返回false。 Can someone help me with the javascript regular expression? 有人可以通过javascript正则表达式帮助我吗? I tried searching a lot. 我尝试了很多搜索。 But I could not find any help for my specific case. 但是对于我的具体案例,我找不到任何帮助。

So this should return characters for cases like
12.09a23
aa12.12
abcd

Update 更新资料

I donot want to see if the input string has floating point number. 我不想看输入字符串是否有浮点数。 I want to see if the input has anything other than floating point number. 我想看看输入是否有除浮点数以外的任何内容。 For example, if the input has 12.3aa23, I want to show the input has aa so this is an invalid input. 例如,如果输入具有12.3aa23,我想显示输入具有aa,因此这是无效的输入。

function isDigit(str){
var n = str.search(/^(\d)*(\.)*(\d)*$/); 
return (n!==-1);
}

from here and here :-) 这里这里 :-)

jsfiddle jsfiddle

You can use /^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$/ regex to find floating point numbers in individual strings (thus, adding anchors). 您可以使用/^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$/正则表达式来查找浮点数在单个字符串中(因此,添加锚点)。 Source 资源

Also, here is a demo . 另外,这是一个演示

Snippet: 片段:

 if ("12.09a23".search(/^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$/) != -1) { alert("Floating number detected! ") } else { alert("Floating number not detected! Invalid data inside string: " + "12.09a23".replace(/[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?/g, '')) } 

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

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