jquery form validation ajaxSubmit()

23,756

I'm not really familiar with the validation plugin, but I would try it the other way around, by validating the form in the form plugin's beforeSubmit event:

$('#contact').ajaxForm({
  beforeSubmit: function() {
    $('#contact').validate({ /* ... */ });
    return $('#contact').valid();
  },
  success: function(resp) {
    alert(resp);
  }
});
Share:
23,756
robx
Author by

robx

Software QA Engineer, Chico CA I love and have a passion to write, execute and write automated tests, to help make a better product. Mentoring and and developing new and inventive ways to make work easier and effective is always in my mind as next steps to improve. Detail oriented in fast pace environment. Building test tools and test script tools. Team player Metrics collector Love Technology whether it be software or hardware Providing support whether it be a co worker or non technical user Due diligent to complete and or exceed your task on time personality Quality over Quantity, but don't spend too much time that it is holding back productivity Improvise and revise to optimize output Selenium / Java automation testing Manual and exploratory testing Love to create and start new Selenium Automation test tools

Updated on July 09, 2022

Comments

  • robx
    robx almost 2 years

    I am getting very frustrated with this jquery form validation ajaxSubmit();

    In chrome, it seems to fail silently without even hitting the submitHandler, firefox does, but still goes on to the action="contact.php" page. I was expecting it to just post in the background and possibly return results, but can't get it to work that way.

    Edit: Found a fix for this if anyone else is interested, put this line of code somewhere in your dom ready.

    // disable HTML5 native validation and let jquery handle it.
        $('form').attr('novalidate','novalidate');
    

    HTML form:

    <form method="post" action="contact.php" id="contact">
        <fieldset>
            <input type="text" id="email" class="required email" name="email"  maxlength="30" value="youremail @ example.com" />
            <input type="text" id="subject" class="required" name="subject" maxlength="24" value="Enter your subject" />
            <textarea id="msg" class="required textarea" name="msg" cols="30" rows="5">Reason for contact.</textarea>
            <input type="submit" class="formBtn" value="Punch me an Email" />
        </fieldset>
    </form>
    

    JQuery:

    $(window).load(function(){
        $('#contact').validate({
                rules:{
                    email:{required: true, email:true},
                    subject:{required: true, minlength: 5},
                    msg:{required: true, minlength: 50}
                },
                messages:{
                    email:'',
                    subject: '',
                    msg: ''
                },
                invalidHandler:function(){
                    $('div.error').hide();
                },
                focusInvalid:true,
                onfocusout:false,
                submitHandler: function() {
                   // never hits with chrome, and alert works in FF4, 
                   // but still goes to contact.php
                   $('#contact').ajaxSubmit();
               }
            });
    });
    
  • robx
    robx about 13 years
    docs.jquery.com/Plugins/Validation/validate#options I have been up and down this document page and i did it the way they showed. Even tried wrapping it in a $('#contact').submit(function(){}), but no go.
  • robx
    robx about 13 years
    what? If you do it that way, the script is gonna try to ajaxSubmit before it even validates. Basically it's gonna try to ajaxsubmit every time the page loads or refreshes.
  • robx
    robx about 13 years
    Thanks. Since I found the root cause of the form not validating, your method works like a charm.