var cost_price = "15..00"
/*Cost price should be such that it should contain numbers and may not contain more than one dot*/
if(/^([0-9])|([.])/.test(cost_price)){
documet.write("Correct cost price");
}现在,尽管cost_price中有两个点,我还是得到了问候消息。我应该在if条件中更改什么?
附言:我已经合并了2个注册表。一种是检查数字的正确性,另一种是检查点是否只出现一次。
发布于 2014-04-22 01:06:01
为什么不去
/^[0-9]+(\.[0-9]+)?$/并因此使最后一部分完全可选?( ?指定“匹配0到1次”)

如果您希望允许.15,您可以将第一个[0-9]+ (匹配1到无限次)更改为[0-9]* (匹配0到无限次)。
发布于 2014-04-22 01:06:10
对于您的情况,regex应该是这样的:
/^\d+(\.\d+)?$/发布于 2014-04-22 01:11:18
var cost_price = "15..00"
if (/\d+\.\d+/.test(cost_price)) {
documet.write("Correct cost price");
} else {
documet.write("Incorrect cost price");
}
http://regex101.com/r/cL8pP1说明:
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “.” literally «\.»
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»https://stackoverflow.com/questions/23201994
复制相似问题