jQuery Validation using the class instead of the name value

前端 未结 7 1283
清酒与你
清酒与你 2020-11-29 00:09

I\'d like to validate a form using the jquery validate plugin, but I\'m unable to use the \'name\' value within the html - as this is a field also used by the server app. Sp

7条回答
  •  执笔经年
    2020-11-29 00:44

    If you want add Custom method you can do it

    (in this case, at least one checkbox selected)

    
    

    in Javascript

    var tags = 0;
    
    $(document).ready(function() {   
    
        $.validator.addMethod('arrayminimo', function(value) {
            return tags > 0
        }, 'Selezionare almeno un Opzione');
    
        $.validator.addClassRules('check_secondario', {
            arrayminimo: true,
    
        });
    
        validaFormRichiesta();
    });
    
    function validaFormRichiesta() {
        $("#form").validate({
            ......
        });
    }
    
    function test(n) {
        if (n.prop("checked")) {
            tags++;
        } else {
            tags--;
        }
    }
    

提交回复
热议问题