10

I have a simple HTML5 form that has no <input type="submit"> because the button to submit the form is outside the form itself. There are several inputs in the form that have the required="required" propert set.

The button outside the form that I will submit it has the following code attached:

$('#upgrade_submit').click(function(){
    $('#cc-form-modal').submit();
    return false;
});

When the button is clicked, it doesn't run the normal HTML5 validation on the required fields. What am I doing wrong? Is there a way to check if the form is valid before "forcing" the submit?

2
  • Related maybe : stackoverflow.com/questions/11866910/… Commented Oct 9, 2013 at 19:18
  • 1
    @Sergio That is very related. The only improvement I would make over the accepted answer would be if I could come up with a solution that wouldn't require HTML5. Commented Oct 9, 2013 at 19:23

1 Answer 1

14

I accomplished my goal by adding a hidden submit input element like this:

<input type="submit" class="submit" style="display:none;">

I then attache the following code to my external submit handler.

$('#upgrade_submit').click(function(){
    $('#cc-form-modal .submit').click();
    return false;
});

This will cause HTML5's validation to run, default error messages to be displayed and all works well!

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

1 Comment

Pure jQuery mess. No other method since 2013?

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.