一个比较完善表单校验插件(续)----自以为算是比较全面的校验规则

潮流服饰屋

$.fn.jStar={};

$.fn.jStar.validations = [];
$.fn.jStar.validation = function(name, func) {
    $.fn.jStar.validations.push(name);
    $.fn.jStar.validations[name] = func;
};


$.fn.jStar.validation('required', function(element, value) {
    if(element.attr('type') == 'checkbox') {
        if(element.attr('checked') == true) return true;
        else return false;
    } else {
        if(value.length == 0) return false;
        else return true;
    }
});




$.fn.jStar.validation('minlength', function(element, value, minlength) {
    if(value.length < minlength) return false;
    else return true;
});


$.fn.jStar.validation('maxlength', function(element, value, maxlength) {
    if(value.length > maxlength) return false;
    else return true;
});


$.fn.jStar.validation('rangelength', function(element, value, minlength, maxlength) {
    if(value.length >= minlength && value.length <= maxlength) return true;
    else return false;
});




$.fn.jStar.validation('min', function(element, value, min) {
    if(parseInt(value) < min) return false;
    else return true;
});


$.fn.jStar.validation('max', function(element, value, max) {
    if(parseInt(value) > max) return false;
    else return true;
});


$.fn.jStar.validation('range', function(element, value, min, max) {
    if(parseInt(value) >= min && parseInt(value) <= max) return true;
    else return false;
});




$.fn.jStar.validation('number', function(element, value) {
    if(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value)) return true;
    else return false;
});


$.fn.jStar.validation('digits', function(element, value) {
    if(/^\d+$/.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('phone', function(element, value) {
    if( /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('mobile', function(element, value) {
    if(/^0?(13[0-9]|15[0-35-9]|18[0236789]|14[57])[0-9]{8}$/g.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('chinese', function(element, value) {
    if(/^[\u0391-\uFFE5]+$/.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('english', function(element, value) {
    if(/^[A-Za-z]+$/.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('zip', function(element, value) {
    if(/^[1-9]\d{5}$/.test(value)) return true;
    else return false;
});
$.fn.jStar.validation('idCard', function(element, value) {
    return isIdCard(value);
});




$.fn.jStar.validation('email', function(element, value) {
    if(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value)) return true;
    else return false;
});




$.fn.jStar.validation('url', function(element, value) {
    if(/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\u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|:|@)|\/|\?)*)?$/i.test(value)) return true;
    else return false;
});




$.fn.jStar.validation('username', function(element, value) {
    if(/^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$/.test(value)) return true;
    else return false;
});




$.fn.jStar.validation('match', function(element, value, match) {
    if($(match).val() != value) return false;
    else return true;
});




$.fn.jStar.validation('date', function(element, value) {
    if(!/Invalid|NaN/.test(new Date(value))) return true;
    else return false;
})
$.fn.jStar.validation('contains', function(element,value, word) {
    return value.indexOf(word) != -1;
})




//function watchSelect(type) {
//    $('input['+$.fn.jStar.defaults.validationAttribute+'*="'+type+'"]').each(function() {
//        var el = $(this);
//
//        $('input[name="'+el.attr('name')+'"]').each(function() {
//            var al = $(this);
//            if(al.attr($.fn.jStar.defaults.validationAttribute).indexOf(type) == -1) al.blur(function() { el.blur(); });
//        });
//    });
//}
//
//$(document).ready(function() {
//    watchSelect('minselect');
//    watchSelect('maxselect');
//    watchSelect('rangeselect');
//});


$.fn.jStar.validation('minselect', function(element, value, min) {
    if($('input[name="'+element.attr('name')+'"]:checked').length >= min) return true;
    else return false;
});


$.fn.jStar.validation('maxselect', function(element, value, max) {
    if($('input[name="'+element.attr('name')+'"]:checked').length <= max) return true;
    else return false;
});


$.fn.jStar.validation('rangeselect', function(element, value, min, max) {
    var checked = $('input[name="'+element.attr('name')+'"]:checked');


    if(checked.length >= min && checked.length <= max) return true;
    else return false;
});


function isIdCard(number){
    var date, Ai;
    number=number.replace("X","x");
    var verify = "10x98765432";
    var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var area = ['','','','','','','','','','','','北京','天津','河北','山西','内蒙古','','','','','','辽宁','吉林','黑龙江','','','','','','','','上海','江苏','浙江','安微','福建','江西','山东','','','','河南','湖北','湖南','广东','广西','海南','','','','重庆','四川','贵州','云南','西藏','','','','','','','陕西','甘肃','青海','宁夏','新疆','','','','','','台湾','','','','','','','','','','香港','澳门','','','','','','','','','国外'];
    var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/i);
    if(re == null) return false;
    if(re[1] >= area.length || area[re[1]] == "") return false;
    if(re[2].length == 12){
        Ai = number.substr(0, 17);
        date = [re[9], re[10], re[11]].join("-");
    }
    else{
        Ai = number.substr(0, 6) + "19" + number.substr(6);
        date = ["19" + re[4], re[5], re[6]].join("-");
    }
    if(!this.isDate(date, "ymd")) return false;
    var sum = 0;
    for(var i = 0;i<=16;i++){
        sum += Ai.charAt(i) * Wi[i];
    }
    Ai +=  verify.charAt(sum%11);
    return (number.length ==15 || number.length == 18 && number == Ai);
}
function isDate(op, formatString){
    formatString = formatString || "ymd";
    var m, year, month, day;
    switch(formatString){
        case "ymd" :
            m = op.match(new RegExp("^((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})$"));
            if(m == null ) return false;
            day = m[6];
            month = m[5]*1;
            year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
            break;
        case "dmy" :
            m = op.match(new RegExp("^(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))$"));
            if(m == null ) return false;
            day = m[1];
            month = m[3]*1;
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
            break;
        default :
            break;
    }
    if(!parseInt(month)) return false;
    month = month==0 ?12:month;
    var date = new Date(year, month-1, day);
    return (typeof(date) == "object" && year == date.getFullYear() && month == (date.getMonth()+1) && day == date.getDate());
    function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}

}

如果有任何bug或遗漏的地方,请大家帮忙指正,万分感激!

潮流服饰屋

你可能感兴趣的:(form,jquery,jQuery,JQUERY,Jquery,validation,validationRules)