In my forms, I'd like to use the new HTML5 form types, for example <input type="url" />
(more info about the types here).
The problem is that Chrome wants to be super helpful and validate these elements for me, except that it sucks at it. If it fails the built-in validation, there's no message or indication other than the element getting focus. I prefill URL elements with "http://"
, and so my own custom validation just treats those values as empty strings, however Chrome rejects that. If I could change its validation rules, that would work too.
I know I could just revert back to using type="text"
but I want the nice enhancements using these new types offers (eg: it automatically switches to a custom keyboard layout on mobile devices):
So, is there a way to switch off or customise the automatic validation?
To ignore HTML validation, you can remove the attribute on button click using JavaScript. Uer removeAttribute() to remove an attribute from each of the matched elements.
If You want to disable the validation for all elements of the form add the novalidate attribute in the form tag which will disable the validation for entire form.
novalidate attribute is used in form tag to disable HTML5 based Form validation.
novalidate if a form-level attribute used to turn off validation for a form, despite the attributes of the inputs it contains (i.e. will override inputs with the required attribute, or that would otherwise fail validation).
If you want to disable client side validation for a form in HTML5 add a novalidate attribute to the form element. Ex:
<form method="post" action="/foo" novalidate>...</form>
See https://www.w3.org/TR/html5/sec-forms.html#element-attrdef-form-novalidate
I had a read of the spec and did some testing in Chrome, and if you catch the "invalid" event and return false that seems to allow form submission.
I am using jquery, with this HTML.
// suppress "invalid" events on URL inputs $('input[type="url"]').bind('invalid', function() { alert('invalid'); return false; }); document.forms[0].onsubmit = function () { alert('form submitted'); };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="url" value="http://" /> <button type="submit">Submit</button> </form>
I haven't tested this in any other browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With