Validate form before submit jquery

有些话、适合烂在心里 提交于 2019-11-27 06:51:45

问题


I have a html form. i am validating my form using jquery.validate.js and it works on submit event. I want to validate this form before submit get fired.

As per my R&D i have tried :

$('#my-form').on('before-submit',function(){
   alert('Before submit performed');
});

I want to validate form Just before firing submit event. Can any one tell me how to do this?

Many Thanks,

M


回答1:


You can mimic the default jQuery plugin behaviour as follows:

Test if the form is valid; if not prevent the default action from occurring using preventDefault():

$(document)
.on('click', 'form button[type=submit]', function(e) {
    var isValid = $(e.target).parents('form').isValid();
    if(!isValid) {
      e.preventDefault(); //prevent the default action
    }
});

EDIT: this comes in handy if you want to fine-tune your validation test i.e. validating some controls conditionally.



来源:https://stackoverflow.com/questions/28123236/validate-form-before-submit-jquery

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