首页 > 技术文章 > js 验证input文本框只能输入数字和小数点

liuhaotian548 2021-12-08 09:31 原文

function check(e) {
var re = /^\d+(?=.{0,1}\d+$|$)/
if (e.value != "") {
if (!re.test(e.value)) {
alert("请输入正确的数字");
e.value = "";
e.focus();
}
}
}

input type="text" id="score" style="width: 50px;" name="score"
onblur="check(this)" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')"

input增加<>即可

推荐阅读