Alicia Brandon
Alicia Brandon

Reputation: 575

Detect html5 validation using javascript

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

Is there any way I can catch html5 validation? Like the first name field is prevented when I click submit, by using javascript, is there a way I can know there's blockage of html5?

Upvotes: 2

Views: 440

Answers (1)

Lukas Safari
Lukas Safari

Reputation: 1968

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.

so, as W3 said it in the below paragraph you can either set the novalidate on form element or add formnovalidate on the submit button to stop html5 from validating the form and get the control of validating it.

just remember, with this action you will loose all validations capabilities and you have to validate every single field by your self.

on the other hand, maybe you just want to know if browser does not support html5 validation. in that case you may use this code:

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

Upvotes: 1

Related Questions