Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable validation of HTML5 form elements

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?

like image 318
nickf Avatar asked Jun 22 '10 04:06

nickf


People also ask

How do I bypass HTML5 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.

How do I turn off HTML validation?

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.

Which of the following is used in form tag to disable HTML5 based form validation?

novalidate attribute is used in form tag to disable HTML5 based Form validation.

Which is the property given to form to skip the HTML5 validations?

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).


2 Answers

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

like image 113
Jakob S Avatar answered Oct 08 '22 23:10

Jakob S


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.

like image 41
Ben Boyle Avatar answered Oct 08 '22 23:10

Ben Boyle