0

I have a field that allows users to search for a store based on either a suburb or a post code. I am using the Jquery validate plugin to validate the value entered in this field. Basically, I want to set the maxlength to 4 ONLY if a number is entered. If a suburb is entered there there doesn't need to be a max length.

Any suggestions for the best approach?

4

1 回答 1

1

您可以使用验证规则,例如

var validator = $('#myform').validate({
    rules: {
        search: {
            required: true,
            maxlength: {
                param: 4,
                depends: function(element){
                    var value = $.trim($(element).val());
                    return /^\d+$/.test(value);
                }
            }
        }
    },
    messages: {}
});

演示:小提琴

于 2013-09-11T04:04:36.917 回答