jQuery validate rule ONLY AT SUBMIT

后端 未结 6 2205
感动是毒
感动是毒 2020-12-09 02:14

Say i have a form that looks up city, state locations. if the user enters an incorrect city, state, i want the form to be able to check the database against the users input

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 02:59

    If you're trying to get a particular validation method to fire ONLY on the submit event while allowing other validation methods to fire normally (e.g., on the onkeyup event), then you can dynamically add the rule when the submit event fires, then remove it after the validation is performed. The ordering of the bindings is important; keep them in order.

    $('#form').bind('submit', function(event) {
        $('#citystate').rules('add', {myCustomAjaxSyncronousCheckAgainstDBRule: true});
        event.preventDefault();
    });
    
    $('#form').validate({
        rules: {
            citystate: { required: true }
        }
    });
    
    $('#form').bind('submit', function(event) {
        $('#citystate').rules('remove', 'myCustomAjaxSyncronousCheckAgainstDBRule');
        event.preventDefault();
    });
    

提交回复
热议问题