【发布时间】:2020-09-22 11:24:05
【问题描述】:
所以我的表单中有一个textarea,它有一个minlength="15" 和maxlength="2000"。但这意味着当用户达到 2000 个字符时,他就不能再打字了。我想要的是用户能够输入更多字符,但在提交时使用客户端验证来检查 textarea 值是否有效。如果不是,我想让它无效,并显示默认的 HTML5 验证气泡。到目前为止,这是我的代码:
textarea {
min-height: 100px;
min-width: 250px;
}
#charLeft {
color: red;
}
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form action="" method="POST">
<textarea placeholder="Describe Your Issue*" aria-placeholder="Describe Your Issue" name="describeIssue" id="describeIssue" class="describeIssue" onkeyup="countChar(this)" minlength="15" maxlength="2000" required></textarea>
<div id="charLeft">0/15</div>
<br />
<input type="submit" />
</form>
<script type="text/javascript">
function countChar(val) {
let length = val.value.length;
if (length > 15 && length < 2000) {
$('#charLeft').html(length + '/2000');
$('#charLeft').css('color', '#27ae60');
} else if (length < 15) {
$('#charLeft').html(length + '/15');
$('#charLeft').css('color', '#e74c3c');
} else {
$('#charLeft').html(length + '/2000');
$('#charLeft').css('color', '#e74c3c');
}
}
</script>
</body>
如上所示,如果达到 2000 个字符的限制,textarea 将不再允许。我希望它超过 2000 个字符,但是当您提交时,如果超过 2000 个字符,请将其标记为无效。我不知道该怎么做,所以我需要一些帮助。我可以接受纯 JavaScript 或 JQuery。提前致谢,感谢您的帮助!
【问题讨论】:
标签: javascript html jquery css validation