Manually trigger HTML validation on button click
Asked Answered
D

5

17

I am trying to handle form validation on button click. It is validating the form but not showing error.

can anyone help me in this?

<form id="the-form" action="#">
    <input type="text" required="required" placeholder="Required text" />
    <input type="email" required="required" placeholder="email" />
    <button id="btn" type="button">Submit</button>
</form>

JavaScript:

$("#btn").on("click", function(){
    if($("#the-form")[0].checkValidity())
    {
        alert('validated');
    }
    else
    {
        //show errors
        return false;
    }
});

http://jsfiddle.net/5ycZz/

Diehard answered 5/9, 2013 at 10:38 Comment(0)
L
6

I've achieved this by doing steps below:

1) I'm having form:

<form>
    <textarea required></textarea>
    <input type="submit" style="display:none;"/>
</form>

<a>Some other button to trigger event</a>

2) Now we have to check if the form is filled correctly:

//this is <a> click event:
if (!$('form')[0].checkValidity()) {
    $('form').find('input[type="submit"]').click();
    return false;
}

This will trigger form sending but won't send because there are errors ;)

It appears that html5 validation errors are displayed on input[type="submit"] click :)

Hope will work for You too! :)

Likable answered 31/7, 2014 at 7:52 Comment(1)
I'd prefer to use your strategy, but also create a submit hook and let that do the actual submit work. Calling event.preventDefault() stops the browser from executing the form action. So the <a> click event would be simply $('form').find('input[type="submit"]').click() and $('form').on("submit", function(event) { event.preventDefault(); /* your code here */})Beltz
R
35

Try

reportValidity()

So you'd have

$("#btn").on("click", function(){
    if($("#the-form")[0].checkValidity()) {
        alert('validated');
    }
    else {
        $("#the-form")[0].reportValidity();
    }
});
Retard answered 19/7, 2017 at 20:50 Comment(1)
perfect! developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/…Reclamation
L
6

I've achieved this by doing steps below:

1) I'm having form:

<form>
    <textarea required></textarea>
    <input type="submit" style="display:none;"/>
</form>

<a>Some other button to trigger event</a>

2) Now we have to check if the form is filled correctly:

//this is <a> click event:
if (!$('form')[0].checkValidity()) {
    $('form').find('input[type="submit"]').click();
    return false;
}

This will trigger form sending but won't send because there are errors ;)

It appears that html5 validation errors are displayed on input[type="submit"] click :)

Hope will work for You too! :)

Likable answered 31/7, 2014 at 7:52 Comment(1)
I'd prefer to use your strategy, but also create a submit hook and let that do the actual submit work. Calling event.preventDefault() stops the browser from executing the form action. So the <a> click event would be simply $('form').find('input[type="submit"]').click() and $('form').on("submit", function(event) { event.preventDefault(); /* your code here */})Beltz
M
4

here is the perfect answer

<form id="the-form" action="#">
<input type="text" required="required" placeholder="Required text" />
<input type="email" required="required" placeholder="email" />
<button id="btn" type="submit">Submit</button>

$("#btn").on("click", function(){
if($("#the-form")[0].checkValidity())
{
    alert('validated');
}
else
{
  return 0;
}

});

Metamorphosis answered 9/12, 2014 at 7:28 Comment(0)
I
0

Remove the else statement. (Update)

$("#btn").on("click", function(){
if($("#the-form")[0].checkValidity())
{
    alert('validated'); //will appear if name and email are of valid formats
}
});
Intarsia answered 5/9, 2013 at 10:50 Comment(4)
i dont want to show errors in alert it should show error in bootstrap popover which is default behaviour of html5Diehard
@ImranRashid I see. Remove the 'else' statement: else { //show errors alert('not validated'); }Intarsia
check on jsfiddle.net/5ycZz.. if i change button type to submit it works otherwise it show no msgDiehard
@ImranRashid The fiddle you gave me has the button type="button" which works. It shows the Validation box.Intarsia
E
-1

You should be able to simply add onclick="return validateForm()".

<button id="btn" onclick="return validateForm()" type="button">Submit</button>
Enceinte answered 8/7, 2015 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.