Search code examples
jqueryhtmlformsvalidationpreventdefault

How after submit form, e.preventDefault(), run some script and continue with html5 validation?


I have classic HTML form. After click on submit button i want run my script so i have in jquery:

$(document).on('click', '.myForm #btnSubmit', function(e) {

    e.preventDefault();

    // some scripts

so before send form i have e.preventDefault();, after this runs my scripts.... And after my scripts are done, i want continue html5 validation like required field etc...

How i can continue? I was tried click() but it is nonsense because it is loop - again have e.preventDefault(); and so on...

In short: Client click on submit button, i need run some my javascript scripts and if results are ok, then i need continue with standart behaviour - html5 validation...


Solution

  • Okay got it!

    Create another hidden input type submit and after run own scripts make trigger click() on it...

    Code:

    HTML:

    <form>
        <input type='text' required>
        <input id='submitHidden' style='display: none' type='submit'>
        <input id='btnSubmit' type='submit'>
    </form>
    

    JQUERY:

    $(document).on('click', '.myForm #btnSubmit', function(e) {
    
        e.preventDefault();
    
        // some scripts
    
        // if some script allow continue with html5 validation then:
    
        $("#submitHidden).click();
    

    jsFiddle