如何让文本框输入的是小于15的数字

weixin_46430414 2020-04-25 08:59:48
<script type="text/javascript">
function chkNumber(eleText) {
var value = eleText.value;
if(value > 15 || value.type!=number) {
eleText.focus();
alert("输入的必须是小于15的数字");
(然后如何让光标回到文本框)
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="">
<label for="we"></label>
<input type="text" name="we" id="we" onblur="chkNumber(this)"/>
</form>
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2020-04-25
  • 打赏
  • 举报
回复
其实你可以直接用 type="number" 数值插入框 <input type="number" max="15" value="0" /> 当大于15在表单提交时会自动提示
天际的海浪 2020-04-25
  • 打赏
  • 举报
回复

  	function chkNumber(eleText) {
  		var value = parseFloat(eleText.value);
  		if (isNaN(value) || value > 15) {
  			alert("输入的必须是小于15的数字");
  			eleText.focus();
  		}
  	}
Coder-F 2020-04-25
  • 打赏
  • 举报
回复

<!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>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧