Form validation in javascript does not work probably

允我心安 提交于 2019-12-20 07:15:03

问题


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.


回答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();
        }
    });
});



回答2:


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();
   }
});



回答3:


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

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



来源:https://stackoverflow.com/questions/11598702/form-validation-in-javascript-does-not-work-probably

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!