12/19/2016 - 2:41 PM

html5 force validity pattern

html5 force validity pattern

To check whether a certain field is valid, use:

$('#myField')[0].checkValidity() // returns true/false
To check if the form is valid, use:

$('#myForm')[0].checkValidity() // returns true/false
If you want to display the native error messages that some browsers have (such as Chrome), unfortunately the only way to do that is by submitting the form, like this:

var $myForm = $('#myForm')
if (!$myForm[0].checkValidity()) {
  // If the form is invalid, submit it. The form won't actually submit;
  // this will just cause the browser to display the native HTML5 error messages.
    $("input[name=Password]")[0].oninvalid = function () {
        this.setCustomValidity("Please enter at least 5 characters.");