HTML5 input required - how to disable

馋奶兔 提交于 2019-12-23 12:29:03

问题


JQuery Validate intercepts the required attribute e.g.

<input type="text" required />

This will force client side validation of the field which is great. Now this also is part of the HTML5 spec and causes browsers that support that to show validation messages even if JavaScript is disabled.

In my application I have already built the server side validation which I am happy with so I want to stop this behaviour. So my question is how can you disable this HTML5 form validation?

Update There seems to be some confusion, basically I am happy with the JQuery validate interpreting that and the validation firing that is great. However, should a user have JavaScript disabled I want the validation to fall back to the server side validation that I have already written. However, because JQuery Validate and HTML5 share the same mark up it is not submitting the form but rather showing the HTML5 validation messages which is what I wish to disable. Does this make sense?


回答1:


Add novalidate parameter in form object to stop browser validating the form.

<form action="test.do" novalidate>
   <input type="text" required />
</form>



回答2:


HTML5 compliant browsers will natively interpret that as a required field, so there isn't really a way to stop it. By adding it you are saying, this is required.

Here's an idea, that you could try. There is another attribute "pattern", where you can provide an expression for validation. Perhaps if you make this accept everything then the HTML5 validation from the browser will just pass everything by and still allow your jquery validation to work.

<input type="url" required pattern="*" />

I have not tested this and it is a theory. If it works, then that is awesome. ;)



来源:https://stackoverflow.com/questions/17223332/html5-input-required-how-to-disable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!