gpt4 book ai didi

javascript - 用户名小于4个字符时如何设置CustomValidity?

转载 作者:行者123 更新时间:2023-12-02 14:14:45 25 4
gpt4 key购买 nike

我有这个简单的 html 和 Jquery 代码。我想将 setCustomValidity 设置为当用户在文本字段中输入 4 个字符时显示。到目前为止,我的代码中还没有得到这样的结果:

<html>
<body>

<input type="text" placeholder="Username" oninvalid="setCustomValidity('Please enter username')" oninput="setCustomValidity('')" id="username" name="username"" required>

</body>
<script>
var username = $("#username").val();

if (username.strlen < 4) {
username.setCustomValidity('Username must contain at least 5 characters');
} else {
username.setCustomValidity('');
}

$(document).ready(function() {
$("#username").keyup(checkUsername);
});
</script>
</html>

我尝试过这个:

<script>
var username = $("#username").val();

if (username.strlen < 4) {
username.setCustomValidity('Username must contain at least 5 characters');
} else {
username.setCustomValidity('');
}

$(document).ready(function() {
$("#username").keyup(checkUsername);
});
</script>

但是这不起作用。有人可以帮忙吗?

最佳答案

friend ,为什么你不使用 html5 模式

    <input type="text" placeholder="Username"  pattern=".{5,}" title="Username must contain at least 5 characters" required id="username" name="username">

注意:现在用户无法在用户文本字段中输入少于 5 个字符

演示:-

<!DOCTYPE html>
<html>
<body>
<form action="youractionpage.php">
<input type="text" placeholder="Username" pattern=".{5,}" title="Username must contain at least 5 characters" required id="username" name="username">
<input type="submit" value="ok">
</form>
</body>

</html>

关于javascript - 用户名小于4个字符时如何设置CustomValidity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com