JavaScriprt校验常见数据格式【验证身份证是否合法】【检查电话格式】【检查邮件格式】【检网址格式】【检QQ格式】

function CheckSFZ(name) {
    //验证身份证是否合法 validateIdCard(obj) 返回值 0-合法 1-非法身份证号  2- 非法地区  3-非法生日
    var value = document.getElementById(name).value;
    if (value != "") {
        if (validateIdCard(value) != "0") {
            alert("身份证号格式错误!");
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}
 
//检查电话格式 包括手机和固定电话
function CheckTel(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs_Phone = /^(\b13[0-9]{9}\b)|(\b15[0-9][0-9]{8}\b)|(\b18[0-9]{9}\b)$/;
        var rs_Tel = /^(\b0\d{8,11}\b)|(\b\d{6,8}\b)|(\b0\d{2,3}\-\d{6,8}\b)$/;
        if (!rs_Phone.test(value) && !rs_Tel.test(value)) {
            alert('电话格式错误');

            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

//检查手机格式
function CheckMobile(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^(\b13[0-9]{9}\b)|(\b15[0-9][0-9]{8}\b)|(\b18[0-9]{9}\b)$/;
        if (!rs.test(value)) {
            alert('手机格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}
//检查固定电话格式
function CheckPhone(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^(\b0\d{8,11}\b)|(\b\d{6,8}\b)|(\b0\d{2,3}\-\d{6,8}\b)$/;
        if (!rs.test(value)) {
            alert('电话格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}

//检查固定电话格式
function CheckChuanZhen(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^(\b0\d{8,11}\b)|(\b\d{6,8}\b)|(\b0\d{2,3}\-\d{6,8}\b)$/;
        if (!rs.test(value)) {
            alert('传真格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}
//检查邮件格式
function CheckEmail(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /\w@\w*\.\w/;
        if (!rs.test(value)) {
            alert('邮件格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}
//检查是否为数字
function CheckNO(name) {
    var value = document.getElementById(name).value;
    if (isNaN(value)) {
        alert('您输入的数据格式不正确,请重新输入');
        document.getElementById(name).value = "";
        document.getElementById(name).focus();
    }
}

//检网址格式
function CheckWeb(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?/gi;
        if (!rs.test(value)) {
            alert('网址格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}

//检QQ格式
function CheckQQ(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^[1-9]\d{4,9}$/;
        if (!rs.test(value)) {
            alert('QQ格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}
//检邮编格式
function CheckYouBian(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^\d{6}$|^\d{3}$/;
        if (!rs.test(value)) {
            alert('邮编格式错误');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}
//检测输入文本的长度
function CheckLengh(name,Max) {
    var value = document.getElementById(name).value;
    var len = value.length;
    if (len == 0) {
        document.getElementById(name + "_Tishi").innerText = "必填";
        document.getElementById(name + "_Tishi").style.visibility = 'visible';
    }
    else if (len > Max) {
        document.getElementById(name + "_Tishi").innerText = "您输入的数据过长,最多可输入" + Max + "个字符";
        document.getElementById(name + "_Tishi").style.visibility = 'visible';
    }
    else {
        document.getElementById(name + "_Tishi").style.visibility = 'hidden';
    }
}

//检测输入的内容已字母开头,只能包含字母、数字及下划线

function CheckSqlInput(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^[a-zA-Z][a-zA-Z0-9_]*$/;
        if (!rs.test(value)) {
            alert('输入内容需已字母开头,且只能包含字母、数字及下划线');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}

//判断是否为正整数 var reg1 =  /^\d+$/;

function CheckInt(name) {
    var value = document.getElementById(name).value;
    if (value != "") {
        var rs = /^\d+$/;
        if (!rs.test(value)) {
            alert('请输入正整数');
            document.getElementById(name).value = "";
            document.getElementById(name).focus();
        }
    }
}

//*****************************************
// 验证身份证是否合法 validateIdCard(obj) 返回值 0-合法 1-非法身份证号  2- 非法地区  3-非法生日
// 验证手机号是否合法 checkmobile(obj) 返回值 0-合法 1-不合法
// 判断是否为纯数字 checknum(name) name为控件ID
// 判断输入的是否为只带有(-和.)的数字   checkmoney(name)  name为控件ID
// 判断输入的是否为月份 (1-12)之间的数字 checkmonth(name) name为控件ID
// 判断输入的是否为日 (1-31)之间的数字 checkday(name) name为控件ID
// 金额小写变大写 atoc(numberValue) numberValue小写金额
//*****************************************

function id(name) {
    return document.getElementById(name);
}

//判断身份证是否合法
function validateIdCard(obj) {
    var aCity = { 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 iSum = 0;
    //var info = "";
    var strIDno = obj;
    var idCardLength = strIDno.length;
    if (!/^\d{17}(\d|x)$/i.test(strIDno) && !/^\d{15}$/i.test(strIDno))
        return 1; //非法身份证号
    if (aCity[parseInt(strIDno.substr(0, 2))] == null)
        return 2; // 非法地区 
    // 15位身份证转换为18位
    if (idCardLength == 15) {
        sBirthday = "19" + strIDno.substr(6, 2) + "-" + Number(strIDno.substr(8, 2)) + "-" + Number(strIDno.substr(10, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"))
        var dd = d.getFullYear().toString() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
        if (sBirthday != dd)
            return 3; //非法生日 
        strIDno = strIDno.substring(0, 6) + "19" + strIDno.substring(6, 15);
        strIDno = strIDno + GetVerifyBit(strIDno);
    }
    // 判断是否大于2078年,小于1900年
    var year = strIDno.substring(6, 10);
    if (year < 1900 || year > 2078)
        return 3; //非法生日
    //18位身份证处理
    //在后面的运算中x相当于数字10,所以转换成a
    strIDno = strIDno.replace(/x$/i, "a");
    sBirthday = strIDno.substr(6, 4) + "-" + Number(strIDno.substr(10, 2)) + "-" + Number(strIDno.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"))
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()))
        return 3; //非法生日
    // 身份证编码规范验证
    for (var i = 17; i >= 0; i--)
        iSum += (Math.pow(2, i) % 11) * parseInt(strIDno.charAt(17 - i), 11);
    if (iSum % 11 != 1)
        return 1; // 非法身份证号
    // 判断是否屏蔽身份证
    var words = new Array();
    words = new Array("11111119111111111", "12121219121212121");
    for (var k = 0; k < words.length; k++) {
        if (strIDno.indexOf(words[k]) != -1) {
            return 1;
        }
    }
    return 0;
}

//判断手机号码是否合法
function checkmobile(obj) {
    var flag = 0;
    if (obj.length != 11) {
        //验证手机号为11位
        flag = 1;
    }
    var reg0 = /^13\d{5,9}$/; //130--139。至少7位
    var reg1 = /^15\d{5,9}$/; //15至少7位
    var reg2 = /^18\d{5,9}$/; //18
    var my = false;
    if (reg0.test(obj)) my = true;
    if (reg1.test(obj)) my = true;
    if (reg2.test(obj)) my = true;
    if (!my) {
        flag = 1;
    }
    return flag;
}
// 判断是否为纯数字  name为控件ID
function checknum(name) {
    if (isNaN(id(name).value)) {
        alert('请输入纯数字');
        id(name).value = "";
        id(name).focus();
    }
}
// 判断输入的是否为金额 name为控件ID
function checkmoney(name) {
    var money = id(name).value;
    if (money != "") {
        if (!(/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/.test(money))) {
            alert('请输入正确的金额');
            id(name).value = "";
            id(name).focus();
        }
    }
}
// 判断输入的是否为月份 (1-12)之间的数字
function checkmonth(name) {
    var month = id(name).value;
    if (isNaN(id(name).value)) {
        alert('请输入纯数字');
        id(name).value = "";
        id(name).focus();
    }
    else {
        if (parseInt(month) > 12 || parseInt(month) < 1) {
            alert('请输入1-12之间的数字');
            id(name).value = "";
            id(name).focus();
        }
    }
}
// 判断输入的是否为日 (1-31)之间的数字
function checkday(name) {
    var day = id(name).value;
    if (isNaN(id(name).value)) {
        alert('请输入纯数字');
        id(name).value = "";
        id(name).focus();
    }
    else {
        if (parseInt(day) > 31 || parseInt(day) < 1) {
            alert('请输入1-31之间的数字');
            id(name).value = "";
            id(name).focus();
        }
    }
}
// 金额小写变大写  
function atoc(numberValue) {
    var numberValue = new String(Math.round(numberValue * 100)); // 数字金额  
    var chineseValue = ""; // 转换后的汉字金额  
    var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字  
    var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位  
    var len = numberValue.length; // numberValue 的字符串长度  
    var Ch1; // 数字的汉语读法  
    var Ch2; // 数字位的汉字读法  
    var nZero = 0; // 用来计算连续的零值的个数  
    var String3; // 指定位置的数值  
    if (len > 15) {
        alert("超出计算范围");
        return "";
    }
    if (numberValue == 0) {
        chineseValue = "零元整";
        return chineseValue;
    }
    String2 = String2.substr(String2.length - len, len); // 取出对应位数的STRING2的值  
    for (var i = 0; i < len; i++) {
        String3 = parseInt(numberValue.substr(i, 1), 10); // 取出需转换的某一位的值  
        if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) {
            if (String3 == 0) {
                Ch1 = "";
                Ch2 = "";
                nZero = nZero + 1;
            }
            else if (String3 != 0 && nZero != 0) {
                Ch1 = "零" + String1.substr(String3, 1);
                Ch2 = String2.substr(i, 1);
                nZero = 0;
            }
            else {
                Ch1 = String1.substr(String3, 1);
                Ch2 = String2.substr(i, 1);
                nZero = 0;
            }
        }
        else { // 该位是万亿,亿,万,元位等关键位  
            if (String3 != 0 && nZero != 0) {
                Ch1 = "零" + String1.substr(String3, 1);
                Ch2 = String2.substr(i, 1);
                nZero = 0;
            }
            else if (String3 != 0 && nZero == 0) {
                Ch1 = String1.substr(String3, 1);
                Ch2 = String2.substr(i, 1);
                nZero = 0;
            }
            else if (String3 == 0 && nZero >= 3) {
                Ch1 = "";
                Ch2 = "";
                nZero = nZero + 1;
            }
            else {
                Ch1 = "";
                Ch2 = String2.substr(i, 1);
                nZero = nZero + 1;
            }
            if (i == (len - 11) || i == (len - 3)) { // 如果该位是亿位或元位,则必须写上  
                Ch2 = String2.substr(i, 1);
            }
        }
        chineseValue = chineseValue + Ch1 + Ch2;
    }
    if (String3 == 0) { // 最后一位(分)为0时,加上“整”  
        chineseValue = chineseValue + "整";
    }
    return chineseValue;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值