js:正则表达式表单校验方法

var $c = {
    'isName': function(name){//姓名校验
        var pat = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
        return pat.test(name)
    },
    'isTele': function(tele){//电话校验
        var pat = /(^\d{7,8}$)|(^0\d{2,3}\d{7,8}$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
        return pat.test(tele)
    },
    'isID': function(code){//身份证校验
        var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
        var tip = "";
        var pass= true;
        if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/.test(code)){
            tip = "身份证号格式错误";
            pass = false;
        }else if(!city[code.substr(0,2)]){
            tip = "地址编码错误";
            pass = false;
        }else{
            if(code.length == 18){//18位身份证需要验证最后一位校验位,∑(ai×Wi)(mod 11)
                code = code.split('');
                var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];//加权因子
                var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];//校验位
                var sum = 0,  ai = 0, wi = 0;
                for (var i = 0; i < 17; i++){
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if(parity[sum % 11] != code[17]){
                    tip = "校验位错误";
                    pass =false;
                }
            }
        }
        return pass;
    },
    'checkLength': function(str, len){//检验文字长度
        len = this.isEmpty(len) ? '20' : len;
        return this.isEmpty(str) ? false : str.length <= len
    },
    'isShortWord': function(word){
        var pat = /^[\u4e00-\u9fa5]{2,20}$/;
        return pat.test(word)
    },
    'isEmpty': function(str){//检验是否为空
        return (str !== null && str !== undefined && this.trim(str) !== "") ? false : true;
    },
    'trim': function(str){//去除左右中间所有空格
        return str == undefined ? "" : str.replace(/(^\s*)|(\s*$)/g, "");
    },
    'contains': function(arr, obj) {
        var i = arr.length
        while (i--) {
            if (arr[i] === obj) {
                return true
            }
        }
        return false
    },
    'isArray': function(obj){
        return (typeof obj=='object') && obj.constructor == Array
    },
    'isString': function(obj){
        return (typeof str=='string') && str.constructor == String
    },
    'isNumber': function(obj){
        return ((typeof obj=='number') && obj.constructor == Number) || (/^[0-9]+.?[0-9]*$/.test(obj))
    },
    'isDate': function(obj){
        return (typeof obj=='object') && obj.constructor == Date
    },
    'isFunction': function(obj){
        return (typeof obj=='function') && obj.constructor == Function
    },
    'isObject': function(obj){
        return (typeof obj=='object') && obj.constructor == Object
    },
    'initUpload': function(selector){
        var dom = $(selector)
        if(!dom)return
        dom.append('')
        dom.append('
选择文件
'
) dom.append('
等待上传
'
) dom.removeAttr("class"); dom.attr("class","webuploader-element-invisible"); var uploader = WebUploader.create({ auto: true, swf: basePath +'view/Widget/webuploader/0.1.5/Uploader.swf', server: basePath +'base/fileUpload.ui', pick: { id : '#uploadSelectBtn', multiple : false }, resize: false, accept: { title: 'upFile', extensions: 'doc,docx,xls,xlsx,ppt,pdf,htm,html,txt,zip,rar,gz,bz2,7z,gif,jpg,jpeg,png,bmp,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb', mimeTypes: '*' } }); uploader.on('fileQueued', function(file){ $('#statebox').html("上传中..."); }) uploader.on( 'uploadStart', function( file, response ) { $('#statebox').html("准备上传..."); }) uploader.on( 'uploadSuccess', function( file, response ) { $('#uploadUrl').val(response.data); $('#statebox').html("已上传
\'" +response.data + "\' target=blank>点此下载查看"
); }) uploader.on( 'uploadError', function( file ) { $('#statebox').html("上传出错
(请小于10M"
); }) uploader.on("error",function (type){ if (type=="Q_TYPE_DENIED"){ $('#statebox').html("请检查文件类型是否正确!"); } }) }, 'openUpload': function (selector, postUrl, param, fileArgName){ var dom = $(selector) if(!dom || !postUrl)return dom.removeAttr("class"); dom.attr("class","webuploader-container"); layer.open({ type: 1, title: '上传文件', maxmin: false, shadeClose: true, //点击遮罩关闭层 area : ['400px' , ''], content: dom, btn:['提交','取消'], yes:function(index,layero){ var url = $('#uploadUrl').val() if(isEmpty(url)){ layer.alert('请上传文件', {title : '提示框',icon : 2}); return; } param = param ? param : {} if(fileArgName){ param[fileArgName] = url }else{ param['fileUrl'] = url } layer.confirm('确认上传此文件吗?',function(index){ $.post(postUrl, param, function(data, textStatus, jqXHR){ if(data){ if(1 == data.code){ layer.alert('操作成功!',{title: '提示框',icon:1},function(){window.location.reload();}); }else{ layer.alert('操作失败!' + data.message,{title: '提示框',icon:2}); } }else{ layer.alert('操作失败!',{title: '提示框',icon:2}); } }, 'json' ); }); } }); }, 'isNum':function(obj){ //包含0的正整数 var pat = /^([1-9]\d*|[0]{1,1})$/; return pat.test(obj) }, }

你可能感兴趣的:(正则表达式)