gpt4 book ai didi

javascript - 使用 javascript 检测 html5 验证

转载 作者:行者123 更新时间:2023-11-27 22:51:16 25 4
gpt4 key购买 nike

<form action="">
First name: <input type="text" required name="FirstName" value=""><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>

有什么办法可以捕获 html5 验证吗?就像当我点击提交时名字字段被阻止一样,通过使用 javascript,有没有办法我知道 html5 被阻止了?

最佳答案

The attributes for form submission that may be specified on form elements are action, enctype, method, novalidate, and target.

The corresponding attributes for form submission that may be specified on submit buttons are formaction, formenctype, formmethod, formnovalidate, and formtarget. When omitted, they default to the values given on the corresponding attributes on the form element.

因此,正如 W3 在下一段中所说,您可以在表单元素上设置 novalidate 或在提交按钮上添加 formnovalidate 来阻止 html5 验证表单并获得验证表单的控制权。

请记住,通过此操作,您将失去所有验证功能,并且必须自行验证每个字段。

另一方面,也许您只是想知道浏览器是否不支持 html5 验证。在这种情况下,您可以使用以下代码:

if (typeof document.createElement( 'input' ).checkValidity != 'function')
document.getElementsByTagName("form")[0].setAttribute("novalidate", "")

关于javascript - 使用 javascript 检测 html5 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037276/

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