Skip to content

Instantly share code, notes, and snippets.

@mcshaz
Forked from newhope/reportValidity.js
Last active April 17, 2020 08:57
Show Gist options
  • Select an option

  • Save mcshaz/c65040c11e6daffe356cbedb7ec84ce4 to your computer and use it in GitHub Desktop.

Select an option

Save mcshaz/c65040c11e6daffe356cbedb7ec84ce4 to your computer and use it in GitHub Desktop.

Revisions

  1. mcshaz revised this gist Apr 17, 2020. 1 changed file with 23 additions and 20 deletions.
    43 changes: 23 additions & 20 deletions reportValidity.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,26 @@
    /**
    Only work if your form has a submit button
    */
    if ( ! HTMLFormElement.prototype.reportValidity) {

    HTMLFormElement.prototype.reportValidity = function () {

    var validity = this.checkValidity();

    if ( ! validity) {
    var submitButtons = this.querySelectorAll("button, input[type=submit]");
    if (!HTMLFormElement.prototype.requestSubmit) {
    HTMLFormElement.prototype.requestSubmit = function() {
    const submitBtn = document.createElement('input');
    submitBtn.type = 'submit';
    submitBtn.hidden = true;
    this.appendChild(submitBtn);
    submitBtn.click();
    this.removeChild(submitBtn);
    };
    }

    for (var i = 0; i < submitButtons.length; i++) {
    if (submitButtons[i].type === "submit") {
    submitButtons[i].click();
    return validity;
    if (!HTMLFormElement.prototype.reportValidity) {
    HTMLFormElement.prototype.reportValidity = function() {
    if (this.checkValidity()){
    return true;
    }
    }
    }

    return validity;
    }
    if (this.noValidate){
    this.noValidate = false;
    this.requestSubmit();
    this.noValidate = true;
    } else {
    this.requestSubmit();
    }
    return false;
    };
    }
  2. @newhope newhope created this gist Nov 21, 2018.
    23 changes: 23 additions & 0 deletions reportValidity.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /**
    Only work if your form has a submit button
    */
    if ( ! HTMLFormElement.prototype.reportValidity) {

    HTMLFormElement.prototype.reportValidity = function () {

    var validity = this.checkValidity();

    if ( ! validity) {
    var submitButtons = this.querySelectorAll("button, input[type=submit]");

    for (var i = 0; i < submitButtons.length; i++) {
    if (submitButtons[i].type === "submit") {
    submitButtons[i].click();
    return validity;
    }
    }
    }

    return validity;
    }
    }