21

Is there any way to catch a form's submit action before HTML5 validation steps in?

This is what I have tried:

HTML

<form method='post' >
    <fieldset>
        <input type='email' name='foo' required />
        <input type='submit' />
    </fieldset>
</form>

jQuery

$('form').submit(function() {
    $('fieldset').addStyle('error');
    return false;
})

But the submit is only triggered once the HTML5 validation passes.

1
  • 1
    Do you need the HTML5 validation? If not, you could always disable it with novalidate. Commented Feb 28, 2013 at 14:45

4 Answers 4

9

Nope, the HTML5 validation happens before any submit event, in all browsers, so it's not possible, other than changing the event type, like a click on the button :

$('input[type="submit"]').on('click', function() {
    $('fieldset').addClass('error');
});

FIDDLE

Sign up to request clarification or add additional context in comments.

3 Comments

Cool - looks exactly what I am looking for.
actually you missing something from the code. Add the "e" in the function(e) and inside the function in the first line e.preventDefault; otherwise the form will submit. Then when you are ready add at the end of the function $('form').submit()
@JorgeCode - As the input field is required the form won't submit if it's not filled in, but the click function on the submit button will still register and add the class. There is no point in adding an error class to a form that is submitted, so the code above does exactly what it is supposed to do.
7

Use jquery to disable the validation on the form when the page first loads so you can prevent any validation from occurring before your onsubmit handler has run:

$(document).ready(function(){
   $('form').attr('novalidate','novalidate');
})

and then at the end of your onsubmit function, manually run the HTML5 validation on each of the fields via javascript (using checkValidity())

Comments

0

Change the button from

<input type='submit' />

to a regular button

<input type='button' id="submit" />

And after that, just capture the click event and do the submit yourself

$('#submit').on('click', function() {
    $('fieldset').addStyle('error');

    // if everything is ok
    $('form').submit(); 
})

1 Comment

Yes, I thought about that but dismissed the idea as it breaks the normal behavior of the form, for example ENTER does not submit pr default
0

You can intercept the "invalid" event for that element, apply the class and then return false to cancel it

https://developer.mozilla.org/en-US/docs/Web/Events/invalid

Also I added a focus and keydown event to remove the class.

$('form').submit(function() {
    $('fieldset').addStyle('error');
    return false;
})
$('input').on("invalid", function(e){
   //Code: Action (like ajax...)
   $(e.target).addClass("error");
   return false;
});
$('input').on("focus", function(e){
   //Code: Action (like ajax...)
   $(e.target).removeClass("error");
});
$('input').on("keydown", function(e){
   //Code: Action (like ajax...)
   $(e.target).removeClass("error");
});
.error {
    border: 2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='post' >
    <fieldset>
        <input type='email' name='foo' required />
        <input type='submit' />
    </fieldset>
</form>

Note that it may have the same limitations as HTMLFormElement.reportValidity()

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reportValidity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.