覆盖 html5 验证
我正在开发一个简单的登录表单,有两个字段:
<form>
<input type="email" name="email" required />
<input type="password" name="password" required />
<button type="submit">Log in</button>
</form>
对于现代浏览器,会自动触发验证。
但是,如果Javascript可用,我想接管html5表单验证,并自己处理所有事情。
我想自动验证“onblur”(仅受影响的字段),并且我想在单击“提交”时验证所有字段。
然而,onblur 事件工作正常。当按下“提交”时,不会触发标准“提交”事件。但是,会触发“无效”事件;但仅适用于第一个无效事件。
有什么好方法可以告诉浏览器忽略所有与 HTML5 相关的验证并接管整个过程?
I'm working on a simple login form, with two fields:
<form>
<input type="email" name="email" required />
<input type="password" name="password" required />
<button type="submit">Log in</button>
</form>
For modern browsers, validation is automatically triggered.
However, if Javascript is available, I want to take over the html5 form validation, and handle everything myself.
I want to validate automatically 'onblur' (only the affected field) and I want to validate all fields when 'submit' is clicked.
The onblur events work fine, however.. When 'submit' is pressed, the standard 'submit' event is not triggered. However, an 'invalid' event is triggered; but only for the first invalid event.
What's a nice way to tell the browser to ignore all HTML5-related validation, and take over the entire process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请注意,尽管某些浏览器可能支持 INPUT 元素上的 novalidate 属性,但 HTML 代码不会验证。
根据文档,没有这样的属性。只有 FORM 元素可以包含 novalidate 属性。
Please be advised that — even though some browsers may support the attribute novalidate on INPUT elements — the HTML code does not validate.
According to documentation there is no such attribute. Only the FORM element may contain the novalidate attribute.
这个答案是不正确的。请看willydee的回答。
简单,只需将novalidate
属性添加到您不希望浏览器验证的任何元素,如下所示:由于您希望仅在 JavaScript 可用时取消验证,因此添加使用 JavaScript(使用 jQuery 作为简化示例)
This answer is incorrect. Please see willydee's answer.
Simple, just add thenovalidate
property to any element you don't want the browser to validate, like so:Since you wish to only cancel validation when JavaScript is available, add it with JavaScript (using jQuery for simplified example)
例如,如果您使用 jQuery,另一个选择是删除该属性。
Another option if you are using jQuery for example is to remove the attribute.
也许您可以使用 javascript 将所有“必需”属性转换为“所需数据”,因为您可以使用 javascript 处理它们并使用您自己的处理程序覆盖 html5 表单验证
Maybe you can use javascript to transform all "required" attributes into "data-required" for you can handle them with javascript and to override html5 form validation with your own handler
如果你想进行有条件的 HTML5 验证操作。
我的条件是,如果用户想保存数据,但当它想发送表单时,我想验证,我想停止验证。
If you want to conditional HTML5 validation operation.
My condition is that I want to stop validation if user want to save there data but when its want to send form I want to validate.