Jquery.Validate 扩展方法

  • 包含空格在内的字符长度的限制
  • $.validator.addMethod('IncludeSpaceLength', function (value, element, maxlen) {
            if (maxlen > 0) {
                var remaining = (maxlen - (parseInt($(element).val().replace(/(\r\n|\n|\r)/gm, '\r\n').length)));
                if (remaining < 0) {
                    return false;
                }
            }
            return true;
        }, $.validator.format("Please enter no more than {0} characters."));
  • 电话号码的限制
  • $.validator.addMethod('TelephoneCheck', function (value, element) {
            return this.optional(element) || /^[0-9\-]+$/.test(value);
        }, $.validator.format("Invalid telephone number."));

引用格式为:

telephone: {
required: true,
TelephoneCheck: true,
IncludeSpaceLength: 30
},

posted @ 2015-08-11 19:14  cherry.zhou  阅读(263)  评论(0编辑  收藏  举报