1、说明
本篇文章介绍怎么使用js限制文本框只能输入数字
2、HTML代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function validationNumber(e, num) {
var regu = /^[0-9]+\.?[0-9]*$/;
if (e.value != "") {
if (!regu.test(e.value)) {
alert("请输入正确的数字");
e.value = e.value.substring(0, e.value.length - 1);
e.focus();
} else {
if (num == 0) {
if (e.value.indexOf('.') > -1) {
e.value = e.value.substring(0, e.value.length - 1);
e.focus();
}
}
if (e.value.indexOf('.') > -1) {
if (e.value.split('.')[1].length > num) {
e.value = e.value.substring(0, e.value.length - 1);
e.focus();
}
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" onkeyup="validationNumber(this,0)" name="id1" value="" />
</div>
</form>
</body>
</html>