10

I have a HTML5 form with required fields:

<form id="my_form">
    <input type="text" name="myfield1" required>
    <input type="text" name="myfield2" required>
    <input type="text" name="myfield3" required>
    <button type="button" id="my_button">Check</button>
</form>

Also, I have the following jQuery code:

$('#my_button').on('click', function() {
    if ($('#my_form').checkValidity() == false) {
       //show errors
    }
});

I would like to force show HTML5 errors in //show errors line.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Amparo
  • 784
  • 1
  • 4
  • 21

5 Answers5

12

If you want to show the validation bubbles, at first the form should be submitted. Change the type of the button to submit and listen to submit event.

Also note that jQuery doesn't have checkValidity method, you should at first get the raw DOM element:

// on submit event browser will validate the form and show the bubbles
$('#my_form').on('submit', function() {
    if (this.checkValidity() == false) {
        return false;
    }

   // ...

});

http://jsfiddle.net/m5duh44x/

Ram
  • 143,282
  • 16
  • 168
  • 197
  • Sorry, I downvoted this by accident and I can't undo it! Ahhhh. – Aine Jul 04 '16 at 15:40
  • @Aine It doesn't matter. I was thinking which part of the answer was wrong :) I'll re-edit the answer and then you can re-vote. – Ram Jul 04 '16 at 16:02
  • I tried this method and it worked perfectly and got me further along with my problem on google sheets sidebar validation. The only problem which I need to workout is when I hit the submit button with valid information it goes off into the web. – sk65plus Jul 06 '21 at 18:39
  • If form contains validation error, I don't think it even calls the submit() method. Try putting a `console.log('validity is false')` inside the if and see. – Mingtao Sun Sep 13 '21 at 06:44
0

What about adding a prompt field inside form and change its HTML value depending on error?

HTML

<form id="my_form">
    <div id="prompt"></div>
    <input type="text" name="myfield1" required>
    <input type="text" name="myfield2" required>
    <input type="text" name="myfield3" required>
    <button type="button" id="my_button">Check</button>
</form>

JS

$('#my_button').on('click', function() {
    if ($('#my_form').checkValidity() == false) {
       $('#prompt').removeClass().addClass('error').html('your error message');
    }
});

Of course, you can use this div to prompt both success and error messages. To perform it, you can create proper CSS styles i.e.

CSS

.success {
   background-color:green;
}
.error {
   background-color:red;
}

and assign the proper class depending on your needs (see snippet above).

Giorgio
  • 1,940
  • 5
  • 39
  • 64
0

Just add a div responsible for error messages...

<form id="my_form">
  <input type="text" name="myfield1" required>
  <input type="text" name="myfield2" required>
  <input type="text" name="myfield3" required>
  <button type="button" id="my_button">Check</button>
</form>
<div id="errors"></div>

//JS Part

$('#my_button').on('click', function() {
 if ($('#my_form').checkValidity() == false) {
    //show errors in the error DIV
    $("#errors").empty();
    $("#errors").text("error message");
 }
});
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
mboeckle
  • 938
  • 13
  • 29
0

I have the same problem, html5 bubbles do not appear when you capture the on.click event and do something with it, for example to continue with ajax after the html5 validation.

I have observed that if there is any code (a simple alert) in the capture of the click event, it is not shown the validation errors of html5 corresponding to each field (the bubbles)

It is intended that the validation of existing html5 works (that this is not lost and work correctly) and that greater than that can capture the click event and continue doing operations from javascript

HTML5 whith require inputs where bubble must apear!

  <form id='form_insertar' method="post">
  <div class="modal-body">
    <div class="form-group">
      <label for="first_name">First Name</label>
      <input type="text" id="first_name" placeholder="First Name" class="form-control" maxlength="100" required />
    </div>

    <div class="form-group">
      <label for="last_name">Last Name</label>
      <input type="text" id="last_name" placeholder="Last Name" class="form-control" required />
    </div>

    <div class="form-group">
      <label for="email">Email Address</label>
      <input type="text" id="email" placeholder="Email Address" class="form-control" required/>
    </div>      
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <button type="submit" id='btn_addRecord' class="btn btn-primary">Add Record</button>        
  </div>
  </form>

JavaScript code that makes it isn´t appear:

$(document).ready(function(){
    $('#btn_addRecord').on("click", function(event){
        //event.preventDefault();
        if ($("#form_insertar")[0].checkValidity()) {
            console.log('Validado el form: Inserta los datos con ajax!');
              //addRecord();
            return true;
        }
        if (!$("#form_insertar")[0].checkValidity()) {
            console.log('No validado el form: Devolvemos false y no hacemos nada. (LAMENTABLEMENTE HAY ALGO DESCONOCIDO POR LO QUE NO SE MUESTRAN LOS BUBBLES o ERRORES)');    
            return false;
        }
        //alert('test');
    });

});

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
  • Thank you, I have realized that the answer to this problem was in the previous answer that you have provided-> "If you want to show the validation bubbles, at first the form should be submitted. Change the type of the button to submit and listen to submit event. " Thank you!! – Robert Learning Nov 18 '18 at 22:00
0

Update (2022)

Just use reportValidity on the form that you want to validate and it will show you the error bubbles.

document.forms['myform'].reportValidity();
Eran Betzalel
  • 4,105
  • 3
  • 38
  • 66