JavaScript验证

<script type="text/javascript">
    /*密码*/
    function password()
    {
        var password = document.getElementById("password").value;
        var ts = document.getElementById("tsPassword");
        if (password.length > 18 || password.length < 6 && password.length > 0)
        {
            ts.innerHTML = "密码为6到18位字符";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (password === "")
        {
            ts.innerHTML = "请输入您的密码";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "密码格式正确";
            ts.style.color = "#049b07";
            $("#submitError").css("display", "none");
            return true;
        }
    }
    /*确认密码*/
    function confirmPassword()
    {
        var password = document.getElementById("password").value;
        var confirmPassword = document.getElementById("confirmPassword").value;
        var ts = document.getElementById("tsConfirmPassword");
        if (password !== confirmPassword && confirmPassword != "")
        {
            ts.innerHTML = "两次密码不一致";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (confirmPassword === "")
        {
            ts.innerHTML = "请再次输入您的密码";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "密码确认通过!";
            ts.style.color = "#049b07";
            $("#submitError").css("display", "none");
            return true;
        }
    }
    /*姓名*/
    function username()
    {
        var username = document.getElementById("username").value;
        var ts = document.getElementById("tsUsername");
        var verc = !(/^[A-Za-z\u4E00-\uFA29]{2,20}$/.test(username));

        if (verc && username !== "")
        {
            ts.innerHTML = "真实姓名只能含汉字、大写字母,且至少两个字";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (username === "")
        {
            ts.innerHTML = "请填写您的姓名";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "姓名格式正确!";
            ts.style.color = "#049b07";
            $("#submitError").css("display", "none");
            return true;
        }
    }
    /*手机*/
    function mobile()
    {
        var mobile = document.getElementById("mobile").value;
        var ts = document.getElementById("tsMobile");
        var verc = !(/^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/.test(mobile));
        if (verc && mobile !== "")
        {
            ts.innerHTML = "手机号码为纯数字,长度11位";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (mobile === "")
        {
            ts.innerHTML = "请输入您的手机号码";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "手机号码可以使用!";
            ts.style.color = "#049b07";
            $("#submitError").css("display", "none");
            return true;
        }
    }
    /*电话*/
    function telephone()
    {
        var telephone = document.getElementById("telephone").value;
        var ts = document.getElementById("tsTelephone");
        var verc = !(/(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(telephone));

        if (telephone.length < 8 || verc && telephone !== "")
        {
            ts.innerHTML = "电话码为纯数字,长度8位";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (telephone === "")
        {
            ts.innerHTML = "请输入您的电话号码";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "电话号码可以使用!";
            ts.style.color = "#049b07";
            $("#submitError").css("display", "none");
            return true;
        }
    }
    /*电子邮件*/
    function email()
    {
        var email = document.getElementById("email").value;
        var ts = document.getElementById("tsEmail");
        var verc = !(/^[\w\-\_\.u4e00-\u9fa5]+@[\w\-\_u4e00-\u9fa5]+(\.[\w\-\_u4e00-\u9fa5]+)+$/.test(email));
        if (verc && email !== "")
        {
            ts.innerHTML = "Email格式不正确";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (email === "")
        {
            ts.innerHTML = "请输入您的邮箱";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            if (CheckUser(document.getElementById("ctl00_ContentPlaceHolder1_txtEmailAddress")))
            {
                ts.innerHTML = "此邮箱可以使用!";
                ts.style.color = "#049b07";
                $("#submitError").css("display", "none");
                return true;
            }
        }
    }
    /*提交*/
    function submit()
    {
        $("#submitError").css("display", "none");
        if (password() && confirmPassword() && username() && mobile() && telephone() && email())
        {
            return true;
        }
        else
        {
            $("#submitError").css("display", "inline");
            return false;
        }
    }

</script> 

 

PS:应用于企业带驾后台系统

 

<script type="text/javascript">
    /*EMAIL*/
    function checkemail()
    {
        var email = document.getElementById("ctl00_ContentPlaceHolder1_txtEmailAddress").value;
        var ts = document.getElementById("check_email");
        illegalemail = !(/^[\w\-\_\.u4e00-\u9fa5]+@[\w\-\_u4e00-\u9fa5]+(\.[\w\-\_u4e00-\u9fa5]+)+$/.test(email));
        if (illegalemail && email !== "")
        {
            ts.innerHTML = "Email格式不正确";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (email === "")
        {
            ts.innerHTML = "请输入您的邮箱";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            if (CheckUser(document.getElementById("ctl00_ContentPlaceHolder1_txtEmailAddress")))
            {
                ts.innerHTML = "此邮箱可以使用!";
                ts.style.color = "#049b07";
                return true;
            }
            // ts.innerHTML ="此邮箱可以使用!";
            // ts.style.color="#049b07";
            // return true;
        }
    }
    /*账户密码*/
    function checkpassword()
    {
        var postcode = document.getElementById("ctl00_ContentPlaceHolder1_txtPassword").value;
        var ts = document.getElementById("check_password");
        /*var reg1=!(/^[1-9]\d{5}(?!d)$/.test(postcode));*/
        if (postcode.length > 18 || postcode.length < 6 && postcode.length > 0)
        {
            ts.innerHTML = "密码为6到18位字符";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (postcode === "")
        {
            ts.innerHTML = "请输入您的密码";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            ts.innerHTML = "密码格式正确!";
            ts.style.color = "#049b07";
            return true;
        }
    }
    /*密码确认*/
    function checkyzmm()
    {
        var mima = document.getElementById("ctl00_ContentPlaceHolder1_txtPassword").value;
        var mima1 = document.getElementById("ctl00_ContentPlaceHolder1_txtRePass").value;
        var ts = document.getElementById("yzts");
        if (mima !== mima1 && mima1 !== "")
        {
            ts.innerHTML = "两次密码不一致!";
            ts.style.color = "#ff0000";
            return false;
        }
        /*else if(mima1.length < 6 || mima1.length > 18)
        {
        ts.innerHTML ="密码不能少于6位,大于18位!";
        ts.style.color="#ff0000";
        return false;
        }*/
        else if (mima1 === "")
        {
            ts.innerHTML = "请再次输入您的密码";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            ts.innerHTML = "密码确认通过!";
            ts.style.color = "#049b07";
            return true;
        }
    }
    /*手机号码*/
    function checkcellphone()
    {
        var cellphone = document.getElementById("ctl00_ContentPlaceHolder1_txtCellPhone").value;
        var ts = document.getElementById("check_cellphone");
        var reg1 = !(/^1[35]\d{9}$/.test(cellphone));
        if (reg1 && cellphone !== "")
        {
            ts.innerHTML = "手机号码为纯数字,长度11位。";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (cellphone === "")
        {
            ts.innerHTML = "请输入您的手机号码!";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            if (CheckPhone(document.getElementById("ctl00_ContentPlaceHolder1_txtCellPhone")))
            {
                ts.innerHTML = "电话号码可以使用!";
                ts.style.color = "#049b07";
                return true;
            }
            // ts.innerHTML ="电话号码可以使用!";
            // ts.style.color="#049b07";
            // return true;
        }
    }
    /*驾照*/
    //function jiaz(){
    // var yeardd = document.getElementById ("ctl00_ContentPlaceHolder1_ddlYear").value;
    // var monthdd = document.getElementById ("ctl00_ContentPlaceHolder1_ddlMouth").value;
    // var nojiaz=document.getElementById("ctl00_ContentPlaceHolder1_nojiaz");
    // var after=document.getElementById("ctl00_ContentPlaceHolder1_after");
    // var ts=document.getElementById("jiazhaotime");
    // if(nojiaz.checked==false&&(yeardd==""||monthdd=="")&&after.checked==false){
    // ts.innerHTML ="请选择驾照领取时间!";
    // ts.style.color="#666";
    // return false;
    // }
    // if(nojiaz.checked==true||(yeardd!=""&&monthdd!="")||after.checked==true){
    // ts.innerHTML ="请选择驾照领取时间!";
    // ts.style.color="#aaa";
    // return true;
    // }
    //
    // }
    function jiaz()
    {
        var yeardd = document.getElementById("ctl00_ContentPlaceHolder1_ddlYear").value;
        var monthdd = document.getElementById("ctl00_ContentPlaceHolder1_ddlMouth").value;
        //var nojiaz=document.getElementById("nojiaz");
        //var after=document.getElementById("after");
        var ts = document.getElementById("jiazhaotime");
        if (yeardd === "" || monthdd === "")
        {
            ts.innerHTML = "请选择驾照领取时间";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = "请选择驾照领取时间";
            ts.style.color = "#aaa";
            return true;
        }
    }
    /*用户名*/
    function username()
    {
        var name = document.getElementById("ctl00_ContentPlaceHolder1_txtFname").value;
        var ts = document.getElementById("inputusername");
        if (name === "")
        {
            ts.innerHTML = "请填写用户名";
            ts.style.color = "#ff0000";
            return false;
        }
        else
        {
            ts.innerHTML = ""
            return true;
        }
    }
    /*真实姓名*/ 
    function truename()
    {
        var username = document.getElementById("ctl00_ContentPlaceHolder1_txtFname").value;
        var ts = document.getElementById("check_realname");
        var reg1 = !(/^[A-Z\u4E00-\uFA29]{2,20}$/.test(username));
        if (reg1 && username !== "")
        {
            ts.innerHTML = "真实姓名只能含汉字、大写字母,且至少两个字";
            ts.style.color = "#ff0000";
            return false;
        }
        else if (username === "")
        {
            ts.innerHTML = "请填写您的姓名";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            ts.innerHTML = " ";
            ts.style.color = "#666";
            return true;
        }
    }
    /*证件号码*/
    function checkvoucher()
    {
        var name = document.getElementById("ctl00_ContentPlaceHolder1_txtIdCardNo").value;
        var ts = document.getElementById("check_voucher");
        if (name === "")
        {
            ts.innerHTML = "请输入您的证件号码";
            ts.style.color = "#666";
            return false;
        }
        else
        {
            ts.innerHTML = ""
            return true;
        }
    }
    function submi()
    {
        if (checkemail() && checkpassword() && checkyzmm() && checkcellphone() && jiaz() && truename() && checkvoucher())
        {
            parent.document.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").disabled = false;
            return true;
        }
        else
        {
            parent.document.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").disabled = true;
            return false;
        }
    }
    function CheckUser(txtUser)
    {
        var ts = document.getElementById("check_email");
        var user_name = txtUser.value;
        if (user_name !== "")
        {
            var out = GetXmlResponse("clubregister.aspx?cu=" + user_name);
            if (out === ‘Y‘)
            {
                ts.innerHTML = "邮箱已存在";
                ts.style.color = "#ff0000";
                // document.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").disabled=true;
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    function CheckPhone(txtPhone)
    {
        var ts = document.getElementById("check_cellphone");
        var user_name = txtPhone.value;
        if (user_name !== "")
        {
            var out = GetXmlResponse("clubregister.aspx?phone=" + user_name);
            if (out === ‘Y‘)
            {
                ts.innerHTML = "手机已存在";
                ts.style.color = "#ff0000";
                // document.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").disabled=true;
                return false;
            }
            else
            {
                return true;
            }
        }
    }
</script>

 

PS:提取于嗨友会注册页

 

/*身份证*/
var vcity = { 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: "国外"
};

checktheform = function()
{
    var card = document.getElementById(‘code‘).value;
    var ts = document.getElementById("code");
    //是否为空
    if (card === ‘‘)
    {
        ts.style.border = "solid 1px #ff0000";
        document.getElementById(‘code‘).focus;
        return false;
    }
    //校验长度,类型
    if (isCardNo(card) === false)
    {
        ts.style.border = "solid 1px #ff0000";
        document.getElementById(‘code‘).focus;
        return false;
    }
    //检查省份
    if (checkProvince(card) === false)
    {
        ts.style.border = "solid 1px #ff0000";
        document.getElementById(‘code‘).focus;
        return false;
    }
    //校验生日
    if (checkBirthday(card) === false)
    {
        ts.style.border = "solid 1px #ff0000";
        document.getElementById(‘code‘).focus();
        return false;
    }
    //检验位的检测
    if (checkParity(card) === false)
    {
        ts.style.border = "solid 1px #ff0000";
        document.getElementById(‘code‘).focus();
        return false;
    }
    ts.style.border = "solid 1px #049b07";
    return true;
};


//检查号码是否符合规范,包括长度,类型
isCardNo = function(card)
{
    //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
    var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
    if (reg.test(card) === false)
    {
        return false;
    }

    return true;
};

//取身份证前两位,校验省份
checkProvince = function(card)
{
    var province = card.substr(0, 2);
    if (vcity[province] == undefined)
    {
        return false;
    }
    return true;
};

//检查生日是否正确
checkBirthday = function(card)
{
    var len = card.length;
    //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
    if (len === ‘15‘)
    {
        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
        var arr_data = card.match(re_fifteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date(‘19‘ + year + ‘/‘ + month + ‘/‘ + day);
        return verifyBirthday(‘19‘ + year, month, day, birthday);
    }
    //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
    if (len === ‘18‘)
    {
        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
        var arr_data = card.match(re_eighteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date(year + ‘/‘ + month + ‘/‘ + day);
        return verifyBirthday(year, month, day, birthday);
    }
    return false;
};

//校验日期
verifyBirthday = function(year, month, day, birthday)
{
    var now = new Date();
    var now_year = now.getFullYear();
    //年月日是否合理
    if (birthday.getFullYear() === year && (birthday.getMonth() + 1) === month && birthday.getDate() === day)
    {
        //判断年份的范围(3岁到100岁之间)
        var time = now_year - year;
        if (time >= 3 && time <= 100)
        {
            return true;
        }
        return false;
    }
    return false;
};

//校验位的检测
checkParity = function(card)
{
    //15位转18位
    card = changeFivteenToEighteen(card);
    var len = card.length;
    if (len === ‘18‘)
    {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);
        var cardTemp = 0, i, valnum;
        for (i = 0; i < 17; i++)
        {
            cardTemp += card.substr(i, 1) * arrInt[i];
        }
        valnum = arrCh[cardTemp % 11];
        if (valnum === card.substr(17, 1))
        {
            return true;
        }
        return false;
    }
    return false;
};

//15位转18位身份证号
changeFivteenToEighteen = function(card)
{
    if (card.length === ‘15‘)
    {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);
        var cardTemp = 0, i;
        card = card.substr(0, 6) + ‘19‘ + card.substr(6, card.length - 6);
        for (i = 0; i < 17; i++)
        {
            cardTemp += card.substr(i, 1) * arrInt[i];
        }
        card += arrCh[cardTemp % 11];
        return card;
    }
    return card;
};

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。