87,907
社区成员
发帖
与我相关
我的任务
分享
function chkNumber(eleText) {
var value = parseFloat(eleText.value);
if (isNaN(value) || value > 15) {
alert("输入的必须是小于15的数字");
eleText.focus();
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label for="we"></label>
<input type="text" name="we" id="we" onblur="chkNumber()"/>
<p style="color: red;display: none;" id="error">输入的必须是小于15的数字</p>
</form>
<script type="text/javascript">
function chkNumber() {
console.log('1')
const input = document.querySelector('#we')
const error = document.querySelector('#error')
const onlyNum = /^([0-9]+)$/
if (input.value > 14 || !onlyNum.test(input.value)) {
input.focus()
error.style.display = 'block'
} else {
error.style.display = 'none'
}
}
</script>
</body>
</html>