【发布时间】:2017-06-18 05:42:55
【问题描述】:
我正在尝试下面的代码,但正则表达式不起作用,它允许输入框中的所有字符。
要求:输入文本框不应接受除数字和小数点以外的任何其他字符。
<html>
<body>
<input type="text" onkeypress="myfunction(event);"></input>
<script>
function myfunction(e){
var p = new RegExp(/^[0-9]+([.][0-9]+)?$/);
return e.charCode === 0 || p.test(String.fromCharCode(e.charCode));
}
</script>
</body>
</html>
【问题讨论】:
-
使用
^\d+(\.\d+)?$ -
@Jack 这不是它失败的原因。
-
上述小提琴代码不接受小数点:(
标签: javascript html regex