码迷,mamicode.com
首页 > Web开发 > 详细

常用js

时间:2017-10-10 00:10:45      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:datetime   nbsp   cas   检测   检验   pat   attr   出生日期   宁夏   

/**
 * 验证电话号码
 * @param obj
 * @returns
 */
function chgTelephone(obj){
    var value = $(obj).val().replace(/\s+/g,"");

    var reg = /^\d+$/;
    if(value != ""){
        if(!reg.test(value) || value.length > 12 || value.length < 6){
            alert("请正确填写您的联系电话!");
            return false;
        } 
    }
    return true;
}

function checkMobileOrTel(obj){
    var value = $(obj).val().replace(/\s+/g,"");
    var mobile = /^1[3|4|5|7|8][0-9]\d{8}$/; 
    var tel = /^((0\d{2,3})-)?(\d{7,8})(-(\d{1,4}))?$/;  // 区号-电话号码-分机号  电话号码必填,其余可选
    var deffer = /^((0\d{2,3})-)?(\d{1})\3\3\3\3\3\3(\3)?(-(\d{1,4}))?$/;  //电话号码不可完全一样,比如:1111111
    if(mobile.test(value) || (tel.test(value) && !deffer.test(value))){
       return true;
    }else{
       alert("请正确填写您的联系电话!");
       return false;
    }
}

function isMobile(value){
    var mobile = /^1[3|4|5|7|8][0-9]\d{8}$/; 
    if(mobile.test(value.replace(/\s+/g,"")) ){
       return true;
    }else{
       return false;
    }
}

/**
 * 验证邮箱地址
 * @param obj
 * @returns
 */
function chgEmail(obj){
    var value = $(obj).val();
    var email = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    if(value != ""){
        if(!email.test(value)){
            alert("请正确填写您的邮箱地址!");
            $(obj).val("");
            return false;
        }
    }
    return true;
}



/**
 * 验证数值输入值是否为有效值
 * @param ele 
 * @param dataType 数值类型 f:小数, d:小数, i:整数
 * @returns {Boolean}
 */
function validNumber(ele, dataType){
    var number = $(ele);
    
    var val = number.val().trim();
    var title = number.attr("title");
    if(val === ""){
        alert(title + "不能为空!");
        return false;
    }
    
    if(isNaN(val)){
        alert(title + "不是有效值!");
        number.val("");
        return false;
    }
    
    var isValid = false;
    switch (dataType) {
        case ‘i‘:
            isValid = (val.indexOf(‘.‘) == -1 && parseInt(val) >= 0);
            break;
        case ‘f‘:
        case ‘d‘:
            isValid = (parseFloat(val) >= 0);
            break;
        default:
            break;
    }
    
    if(!isValid){
        alert(title + "不是有效值!");
        number.val("");
        return false;
    }
    return true;
}


/**
 * 验证小数
 * @param number 数字
 * @param length 总长度(不包含小数点, 默认9)
 * @param precision 最大精度(>=0, 默认2)
 */
function validDecimal(number, length, precision){
    if(!length || !/^[1-9]\d{0,}$/.test(length)){
        length = 9;
    }
    if(!precision || !/^[1-9]\d{0,}|0$/.test(precision)){
        precision = 2;
    }
    var regExp = new RegExp("^[1-9]\\d{0," + (length - 1) + "}(\.[0]+)?$");
    if(regExp.test(number)) return true;
    for(var i = 1; i <= precision; i++){
        regExp = new RegExp("^([1-9]\\d{0," + (length - 1 - i) + "}|0)\\.\\d{" + i + "}$");
        if(regExp.test(number)) return true;
    }
    return false;
}

//校验时间格式
function patternDate(Gdate){
    var obj=$(Gdate);
    if(obj.val()!=""){
        var reg=/^(\d{4})-(\d{1}|0\d{1}|1[0-2])-(\d{1}|0\d{1}|[12]\d{1}|3[01])$/;
        if(!reg.test(obj.val().trim())){
            alert("请输入正确的时间(格式:2008-08-08)");
            obj.val("");
            return false;
        }else{
            var date=parseDate(obj.val());
            obj.val(formatterDate(date));
        }
    }
    return true;
}

/**
 * 全选/反选
 * @param select 全选/反选的CheckBox控件
 * @param checkboxName 复选框name属性值
 * @param selectType 全选:1/反选:-1
 */
function selectAll(select, checkboxName, selectType){
    
    var _select = $(select);
    var _selector = ‘input[name="‘ + checkboxName + ‘"]‘;
    
    var _selectType = selectType || parseInt(_select.val());
    var _len = $(_selector).size();
    if(_selectType == 1){// 全选
        for(var i = 0; i < _len; i++){
            //$(_selector)[i].checked = _select[0].checked;
            $(_selector)[i].checked = true;
        }
    }else if(_selectType == -1){// 反选
        for(var i = 0; i < _len; i++){
            var checked = $(_selector)[i].checked;
            $(_selector)[i].checked = !checked;
        }
    }
}

/**
 * 设置默认值(为空)
 * @param val
 * @param defVal
 * @returns
 */
function setDefValue(val, defVal){
    if(val == undefined || typeof val === "undefined" || val == null || val === "undefined" || new String(val).trim() === "") {
        return defVal;
    }
    return val;
}

/**
 * 计算两个日期之间的天数
 * @param date1
 * @param date2
 */
function daysDiffDate(date1, date2){
    
    var _date1 = date1;
    if(typeof date1 == ‘string‘){
        _date1 = parseDate(date1);
    }
    var _date2 = date2;
    if(typeof date2 == ‘string‘){
        _date2 = parseDate(date2);
    }
    
    var days = Math.ceil((_date1 - _date2)/(24 * 60 * 60 * 1000));
    return days;
}

/**
 * 计算日期之间的月份
 */
function dateDiff(startDate, endDate){

    var _startDate = parseDate(startDate);
    var _endDate = parseDate(endDate);
    
    if(_startDate > _endDate){
        return 0;
    }
    
    var _start_year = _startDate.getFullYear();
    var _start_month = _startDate.getMonth();
    var _start_Date = _startDate.getDate();
    
    var _end_year = _endDate.getFullYear();
    var _end_month = _endDate.getMonth();
    var _end_Date = _endDate.getDate();
    
    var _diff_year = _end_year - _start_year;
    var _diff_month = _end_month - _start_month;
    
    var diffMonth = _diff_year * 12 + _diff_month;
    _startDate.setFullYear(_start_year, _start_month + diffMonth, _start_Date);
    if(_startDate > _endDate){
        diffMonth--;
    }
    
    return diffMonth >= 0 ? diffMonth : 0;
}

/**
 * 格式化日期字符串/日期对象
 * @param datetime
 * @param pattern
 * @returns {String}
 */
function formatterDatetime(datetime, pattern){
    var _d = new Date();
    if(typeof datetime == ‘object‘){
        _d = datetime;
    }else if(typeof datetime == ‘string‘){
        _d = parseDate(datetime);
    }
    
    var y = _d.getFullYear();
    var M = _d.getMonth() + 1;
    var d = _d.getDate();
    var h = _d.getHours();
    var m = _d.getMinutes();
    var s = _d.getSeconds();
    
    var yyyy = y;
    var MM = (M < 10 ? (‘0‘ + M) : M);
    var dd = (d < 10 ? (‘0‘ + d) : d);
    var HH = (h < 10 ? (‘0‘ + h) : h);
    var mm = (m < 10 ? (‘0‘ + m) : m);
    var ss = (s < 10 ? (‘0‘ + s) : s);
    
    var fmt = new String(pattern).replace(/yyyy/gi, yyyy).replace(/MM/g, MM).replace(/dd/gi, dd).replace(/HH/gi, HH).replace(/mm/g, mm).replace(/ss/gi, ss);
    fmt = fmt.replace(/y/gi, y).replace(/M/g, M).replace(/d/gi, d).replace(/h/gi, h).replace(/m/g, m).replace(/s/gi, s);
    
    return fmt;
}

/**
 * 转化日期字符串
 */
function parseDate(dateStr){
    if(dateStr==‘‘)
        return null;
    var _dateStr = new String(dateStr).replace(/\-/g, ‘/‘).replace(/\.\d+$/, ‘‘);
    var date = new Date(_dateStr);
    if(date.toString() !== ‘Invalid Date‘){
        return date;
    }
    return null;
}


/**
 * 格式化日期字符串/日期对象
 * @param date
 * @returns 返回格式 ‘2012-12-12‘
 */
function formatterDate(date){
    var _d = new Date();
    if(typeof date == ‘object‘){
        _d = date;
    }else if(typeof date == ‘string‘){
        _d = parseDate(date);
    }
    
    var y = _d.getFullYear();
    var m = _d.getMonth() + 1;
    var d = _d.getDate();
    
    return y + ‘-‘ + (m < 10 ? (‘0‘ + m) : m) + ‘-‘ + (d < 10 ? (‘0‘ + d) : d);
}

//身份证验证
function isIDCard(value){
    /*
    var valueStr = value.replace(/\s+/g,"");
    if (valueStr.length == 18) {
        var reg18 = /^[1-9]\d{5}(\d{4}((((0[13578])|(1[02]))(([0-2][0-9])|(3[01])))|(((0[469])|(11))(([0-2][0-9])|(30)))|(02[0-2][0-9])))\d{3}[\dX]$/;
        return reg18.test(valueStr);
    }
    return false;
    */
    
    var idcard=value.replace(/\s+/g,"");
    var area={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 idcard,Y,JYM;
    var S,M;
    var idcard_array = new Array();
    idcard_array = idcard.split("");
    /*地区检验*/
    if(area[parseInt(idcard.substr(0,2))]==null){
        return false;
    }
    /*身份号码位数及格式检验*/
    switch(idcard.length){
        case 15:
            if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
                ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
            } else {
                ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
            }
            if(!ereg.test(idcard)){//15位验证没有通过
                return false;
            }
        break;

        case 18:
            //18位身份号码检测
            //出生日期的合法性检查 
            //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
            //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
            if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
                ereg=/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
            } else {
                ereg=/^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
            }
            if(ereg.test(idcard)){//测试出生日期的合法性
                //计算校验位
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                + parseInt(idcard_array[7]) * 1 
                + parseInt(idcard_array[8]) * 6
                + parseInt(idcard_array[9]) * 3 ;
                Y = S % 11;
                M = "F";
                JYM = "10X98765432";
                M = JYM.substr(Y,1);/*判断校验位*/
                if(M != idcard_array[17]){
                    return false; /*检测ID的校验位false;*/
                }
            } else {
                return false;
            }
        break;

        default:
        return false;
    }
    return true;
    
}

 

常用js

标签:datetime   nbsp   cas   检测   检验   pat   attr   出生日期   宁夏   

原文地址:http://www.cnblogs.com/whstudy/p/7643284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!