gpt4 book ai didi

jquery - YYYY-MM-DD HH 格式的验证日期 :MM format inside textbox ontype

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:36 26 4
gpt4 key购买 nike

我想验证一个需要接受格式的文本框使用 YYYY-MM-DD HH:MM 格式的日期

<!DOCTYPE html>
<html>
<body>

<form action="/action_page_post.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>

<p>If you click submit, without filling out the text field,
your browser will display an error message.</p>

</body>
</html>

如果不是上述格式,它应该变成红色或发光。

enter image description here

最佳答案

这将测试

  • 模式
  • 有效日期

function isValid(str) {
var isPattern = /^20[1-9][0-9]-[0-1][0-2]-[0-3][0-9] [0-2][0-4]:[0-5][0-9]$/.test(str);
if (!isPattern) return false;
var d = new Date(str);
return (!isNaN(d));
}
window.onload=function() {
document.querySelector("input[name=date]").onkeyup=function() {
var val = this.value;
this.className = isValid(val)?"":"glow"
}
}
.glow { background-color:pink }
<form>
<input type="text" name="date" required>
<input type="submit" value="Submit">
</form>

关于jquery - YYYY-MM-DD HH 格式的验证日期 :MM format inside textbox ontype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43061464/

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