jquery 常用校验总结

来源:互联网 发布:sql建立学生关系语句 编辑:程序博客网 时间:2024/06/08 18:34

常用校验


var mString = {    /**    *字符串去空格    */    trim : function(str) {        return str.replace(/(^\s*)|(\s*$)/g, "");    },    /**     * 判断是否是空字符串或null     *      * @param {Object}     *            str     */    isBlank : function(str) {        if (!str || this.trim(str) == "") {            return true;        } else {            return false;        }    },    /**     * 判断是否是空字符串或null     *      * @param {Object}     *            str     */    isNotBlank : function(str) {        if (!str || this.trim(str) == "") {            return false;        } else {            return true;        }    },    /**     * 日期比较     * @param startTime - 开始时间 2016-09-09     * @param endTime - 结束时间  2016-09-10     * @returns {Boolean}     */    compareTime : function(startTime, endTime){        // 开始时间        var start=new Date(startTime.replace("-", "/").replace("-", "/"));        // 结束时间        var end=new Date(endTime.replace("-", "/").replace("-", "/"));        if(end < start){            return false;        }        return true;    },    //创建uuid    getUuid : function(){        return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {            var r = Math.random() * 16 | 0,                v = c == 'x' ? r : (r & 0x3 | 0x8);            return v.toString(16);        });    },    /**     * 格式化字符串     * @param date 日期字符串,new Date()     * @param format 格式化格式如 yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss     * @returns     */    formatDate : function(date, format){        if (!date) return "";        if (!format) format = "yyyy-MM-dd";           switch(typeof date) {               case "string":                if(date.length==8){                    date = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6,8);                }else if(date.length==14){                    date = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6,8)+" "+date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12,14);                }                date = new Date(date);                   break;               case "number":                   date = new Date(date);                   break;           }            if (!date instanceof Date) return;           var dict = {               "yyyy": date.getFullYear(),               "M": date.getMonth() + 1,               "d": date.getDate(),               "H": date.getHours(),               "m": date.getMinutes(),               "s": date.getSeconds(),               "MM": ("" + (date.getMonth() + 101)).substr(1),               "dd": ("" + (date.getDate() + 100)).substr(1),               "HH": ("" + (date.getHours() + 100)).substr(1),               "mm": ("" + (date.getMinutes() + 100)).substr(1),               "ss": ("" + (date.getSeconds() + 100)).substr(1)           };               return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {               return dict[arguments[0]];           });    }};/** * 验证身份证准确性 * @param num * @returns 准确 返回true */function isIdCardError(num){    if ((/^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(num))){        return true;    }else{        return false;    }}/** * 验证电话号码 * @param num * @returns 是 返回true */function isPhone(num){    if ((/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(num))){        return true;    }else{        return false;    }}/** * 验证邮政编码 * @param num * @returns 是 返回true */function isZip(num){    if ((/^[1-9]\d{5}$/i.test(num))){        return true;    }else{        return false;    }}/** * 验证手机号码 * @param num * @returns 是 返回true */function isMobile(num){    if ((/^(13|15|18)\d{9}$/i.test(num))){        return true;    }else{        return false;    }}/** * 验证输入是否是中文 * @param value * @returns 是:true 否:false */function isChina(value){    return /^[\Α-\¥]+$/i.test(value); }/** * 验证是否是英文 * @param value * @returns 是:true 否:false */function isEnglish(value){    return /^[A-Za-z]+$/i.test(value);}/** * 验证是否包含空格和非法字符  * @param data * @returns 含非法字符或者空格返回false,否则true */function isIllegal(value){    return /.+/i.test(value); }
原创粉丝点击