0

I am working on this form which is suppose to validate the form before submitting

$(document).ready(function() {
    $("#form3").validate();
    if ($('#form3').valid()) $('#form3').submit();
});

But the problem is: it prints the (empty fields error) when the form is loaded.


UPDATE : the function is now working Horraaay :) with the following code:

      <script>
      $(document).ready(function(){
      $("#form3").validate();
     });
     </script>

The only problem was the input form name .. I used 'submit' as a name of the input form however before was on different name.

3 Answers 3

1

Put your code on the form submit event. Something like this:

$(document).ready(function()
{
    $("#form3").validate();
    $("#form3").submit(function(event)    
    {
        if (!$(this).valid()) 
        {
            event.preventDefault();
        }
    });
});
3
  • 1
    No need to use both preventDefault and return false. Jul 22, 2012 at 8:16
  • $(this).validate initializes the plugin, I think you want to do it earlier.
    – Esailija
    Jul 22, 2012 at 8:17
  • yes, return false = event.preventDefault + event.stopPropagation
    – Ram
    Jul 22, 2012 at 8:19
0

I think by default after successful validation it will submit the form . However i don't know why you need to resubmit the form.

If you need to submit manually you can use the SubmitHandler place to submit your form.

$("#form3").validate({
   submitHandler: function(form) {       
     form.submit();
   }
});
0

I solved the problem and now it works ... It is a minor error !

I changed the submit button name and id to ( Submit).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.