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();
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

No need to use both preventDefault and return false.
$(this).validate initializes the plugin, I think you want to do it earlier.
yes, return false = event.preventDefault + event.stopPropagation
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();
   }
});

Comments

0

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

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

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.