欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

js 常用各种验证

shiping1 的头像
function checkSubmit()
{
    if(document.addcontent.fromurl!=null)
    {
        document.addcontent.fromurl.value=document.location.href;
    }     
    if(document.addcontent.title.value==""){
        alert("姓名不能为空!");
        document.addcontent.title.focus();
        return false;
    }
    if(document.addcontent.tel.value==""){
        alert("必须填写手机!");
        document.addcontent.tel.focus();
        return false;
    }
    var tel = document.addcontent.tel.value;
    if (tel!=''){
        if(!tel.match(/^1[3589][0-9]{9}$/g)){
            alert('手机号码格式不对!');//验证手机
            document.addcontent.tel.focus();
            return false;
        }    
    }
    var qq = document.addcontent.qq.value;
    if (qq!=''){  
        if(!qq.match(/^[1-9]{1}[0-9]*$/g)){
            alert('QQ格式不对!');//验证手机
            document.addcontent.qq.focus();
            return false;
        }    
    }
    var email = document.addcontent.email.value;
    if(email!=''){
    //php的cakephp正则表达式
            if (!email.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
                alert("邮箱格式不对");//验证邮箱
                document.addcontent.email.focus();
                return false;
            }
    }
    if(document.addcontent.vdcode.value==""){
        alert("验证码不能为空!");
        document.addcontent.vdcode.focus();
        return false;
    }
    document.getElementById('loading').innerHTML="<img src='/templets/default/images/loadingbaoming.gif' >";
    return true;
}



//乐语的弹出报名
function checkSubmitBaoMing()
{
    if(document.addcontentbaoming.fromurl!=null)
    {
        document.addcontentbaoming.fromurl.value=document.location.href;
    }      
    if(document.addcontentbaoming.title.value==""){
    
        alert("姓名不能为空!");
        document.addcontentbaoming.title.focus();
        return false;
    }
    if(document.addcontentbaoming.tel.value==""){
        alert("必须填写手机!");
        document.addcontentbaoming.tel.focus();
        return false;
    }
    var tel = document.addcontentbaoming.tel.value;
    if (tel!=''){
        if(!tel.match(/^1[3589][0-9]{9}$/g)){
            alert('手机号码格式不对!');//验证手机
            document.addcontentbaoming.tel.focus();
            return false;
        }    
    }
    
    /*
    var lxdh = document.addcontent.lxdh.value;
    
    if(lxdh!=''){
            if (!lxdh.match(/^[0]{1}[0-9]{2,3}\-[0-9]{7,8}$/)) {
                alert("联系电话格式不对");//验证电话
                document.addcontent.lxdh.focus();
                return false;
            }
    }
    var cz = document.addcontent.cz.value;
    if(cz!=''){
            if (!cz.match(/^[0]{1}[0-9]{2,3}\-[0-9]{7,8}$/)) {
                alert("传真格式不对");//验证传真
                document.addcontent.cz.focus();
                return false;
            }
    }
    
    if(document.addcontent.addr.value==""){
        alert("必须填写详细地址!");
        document.addcontent.addr.focus();
        return false;
    }
    
    var yzbm = document.addcontent.yzbm.value;
    if(yzbm!=''){
            if (!yzbm.match(/^[1-9]{1}[0-9]{5}$/)) {
                alert("邮政编码格式不对");//判断邮编
                document.addcontent.yzbm.focus();
                return false;
            }
    }
    */
    if(document.addcontentbaoming.email!=null){
        var email = document.addcontentbaoming.email.value;
        if(email!=''){
        //php的cakephp正则表达式
                if (!email.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
                    alert("邮箱格式不对");//验证邮箱
                    document.addcontentbaoming.email.focus();
                    return false;
                }
        }
    }
    
//    if(document.addcontent.vdcode.value==""){
//        alert("验证码不能为空!");
//        document.addcontent.vdcode.focus();
//        return false;
//    }
    
    document.getElementById('loadingbaoming').innerHTML="<img src='/templets/default/images/loadingbaoming.gif' >";
    
    return true;
}

//底 部bottom报名
function checkSubmitBottom()
{
    
    if(document.addcontentZsjz.fromurl!=null)
    {
        document.addcontentZsjz.fromurl.value=document.location.href;
    }      
    if(document.addcontentZsjz.title.value==""){
    
        alert("姓名不能为空!");
        document.addcontentZsjz.title.focus();
        return false;
    }
    
    if(document.addcontentZsjz.tel.value==""){
        alert("必须填写手机!");
        document.addcontentZsjz.tel.focus();
        return false;
    }
    var tel = document.addcontentZsjz.tel.value;
    if (tel!=''){
        if(!tel.match(/^1[3589][0-9]{9}$/g)){
            alert('手机号码格式不对!');//验证手机
            document.addcontentZsjz.tel.focus();
            return false;
        }    
    }
    
    /*
    var lxdh = document.addcontent.lxdh.value;
    
    if(lxdh!=''){
            if (!lxdh.match(/^[0]{1}[0-9]{2,3}\-[0-9]{7,8}$/)) {
                alert("联系电话格式不对");//验证电话
                document.addcontent.lxdh.focus();
                return false;
            }
    }
    var cz = document.addcontent.cz.value;
    if(cz!=''){
            if (!cz.match(/^[0]{1}[0-9]{2,3}\-[0-9]{7,8}$/)) {
                alert("传真格式不对");//验证传真
                document.addcontent.cz.focus();
                return false;
            }
    }
    
    if(document.addcontent.addr.value==""){
        alert("必须填写详细地址!");
        document.addcontent.addr.focus();
        return false;
    }
    
    var yzbm = document.addcontent.yzbm.value;
    if(yzbm!=''){
            if (!yzbm.match(/^[1-9]{1}[0-9]{5}$/)) {
                alert("邮政编码格式不对");//判断邮编
                document.addcontent.yzbm.focus();
                return false;
            }
    }
    */
    if(document.addcontentZsjz.email!=null){
        var email = document.addcontentZsjz.email.value;
        if(email!=''){
        //php的cakephp正则表达式
                if (!email.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
                    alert("邮箱格式不对");//验证邮箱
                    document.addcontentZsjz.email.focus();
                    return false;
                }
        }
    }
    if(document.addcontentZsjz.yzbm!=null){
        var yzbm = document.addcontentZsjz.yzbm.value;
        if(yzbm!=''){
                if (!yzbm.match(/^[1-9]{1}[0-9]{5}$/)) {
                alert("邮政编码格式不对");//判断邮编
                document.addcontent.yzbm.focus();
                return false;
            }
        }
    }    
    if(document.addcontentZsjz.qq!=null){
        var qq = document.addcontentZsjz.qq.value;
        if(qq!=''){
                if (!qq.match(/^[1-9]{1}[0-9]{1,}$/)) {
                alert("QQ号码格式不对");//判断邮编
                document.addcontent.qq.focus();
                return false;
            }
        }
    }
    

    if(document.addcontentZsjz.vdcode.value==""){
        alert("验证码不能为空!");
        document.addcontentZsjz.vdcode.focus();
        return false;
    }
    
    document.getElementById('loadingbottom').innerHTML="<img src='/templets/default/images/loadingbaoming.gif' >";
    return true;
}


//底 部bottom报名
function checkSubmitZsjz()
{
    if(document.addcontentZsjz.fromurl!=null)
    {
        document.addcontentZsjz.fromurl.value=document.location.href;
    }
    document.addcontentZsjz.yearmonthday.value=document.addcontentZsjz.YYYY.value+'-'+document.addcontentZsjz.MM.value+'-'+document.addcontentZsjz.DD.value;
    
    if(document.addcontentZsjz.title.value==""){
    
        alert("姓名不能为空!");
        document.addcontentZsjz.title.focus();
        return false;
    }
    
    if(document.addcontentZsjz.tel.value==""){
        alert("必须填写手机!");
        document.addcontentZsjz.tel.focus();
        return false;
    }
    var tel = document.addcontentZsjz.tel.value;
    if (tel!=''){
        if(!tel.match(/^1[3589][0-9]{9}$/g)){
            alert('手机号码格式不对!');//验证手机
            document.addcontentZsjz.tel.focus();
            return false;
        }    
    }
    if(document.addcontentZsjz.shengfenzhenghao!=null){
        var shengfenzhenghao = document.addcontentZsjz.shengfenzhenghao.value;
        if(shengfenzhenghao!=''){
        //php的cakephp正则表达式
                if (!email.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
                    alert("身份证号格式不对");//身份证号
                    document.addcontentZsjz.shengfenzhenghao.focus();
                    return false;
                }
        }
    }
    
    
    if(document.addcontentZsjz.email!=null){
        var email = document.addcontentZsjz.email.value;
        if(email!=''){
        //php的cakephp正则表达式
                if (!email.match(/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2,4}|museum|travel)$/i)) {
                    alert("邮箱格式不对");//验证邮箱
                    document.addcontentZsjz.email.focus();
                    return false;
                }
        }
    }
    
    if(document.addcontentZsjz.vdcode.value==""){
        alert("验证码不能为空!");
        document.addcontentZsjz.vdcode.focus();
        return false;
    }
    
    document.getElementById('loadingbottom').innerHTML="<img src='/templets/default/images/loadingbaoming.gif' >";
    return true;
    
}

function checkFile(str){
        str=str.toLowerCase(str);
        var strRegex = "(.jpg|.gif|.png|.jpeg)$"; //用于验证图片扩展名的正则表达式
        var re=new RegExp(strRegex);
        if (re.test(str)){
            return (true);
        }
        else{
            alert("禁止的文件扩展名");
            return (false);
        }
}



下面代码是验证身份证号
<script>
//验证身份证号方法
var test=function(idcard){
var Errors=new Array("验证通过!","身份证号码位数不对!","身份证号码出生日期超出范围或含有非法字符!","身份证号码校验错误!","身份证地区非法!");
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:"xinjiang",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 Errors[4];
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))
return Errors[0];
else
return Errors[2];
break;
case 18:
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[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[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 Errors[0];
else
return Errors[3];
}
else
return Errors[2];
break;
default:
return Errors[1];
break;
}
}
//测试代码
var theid=prompt("请输入身份证号码","15位或18位");
if(theid!=""){
document.write(test(theid));
}else{
//theid=prompt("请输入身份证号码","15位或18位");
}
</script>

http://www.jb51.net/article/23651.htm


下面代码是验证身份证号方法2
<script language="javascript" type="text/javascript"> 
function CheckIdCardField(v_card) { 
var reg = /^\d{15}(\d{2}[0-9X])?$/i; 
if (!reg.test(v_card)) { 
return false; 
} 
if (v_card.length == 15) { 
var n = new Date(); 
var y = n.getFullYear(); 
if (parseInt("19" + v_card.substr(6, 2)) < 1900 || parseInt("19" + v_card.substr(6, 2)) > y) { 
return false; 
} 
var birth = "19" + v_card.substr(6, 2) + "-" + v_card.substr(8, 2) + "-" + v_card.substr(10, 2); 
if (!isDate(birth)) { 
return false; 
} 
} 
if (v_card.length == 18) { 
var n = new Date(); 
var y = n.getFullYear(); 
if (parseInt(v_card.substr(6, 4)) < 1900 || parseInt(v_card.substr(6, 4)) > y) { 
return false; 
} 
var birth = v_card.substr(6, 4) + "-" + v_card.substr(10, 2) + "-" + v_card.substr(12, 2); 
if (!isDate(birth)) { 
return false; 
} 
iW = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); 
iSum = 0; 
for (i = 0; i < 17; i++) { 
iC = v_card.charAt(i); 
iVal = parseInt(iC); 
iSum += iVal * iW[i]; 
} 
iJYM = iSum % 11; 
if (iJYM == 0) sJYM = "1"; 
else if (iJYM == 1) sJYM = "0"; 
else if (iJYM == 2) sJYM = "x"; 
else if (iJYM == 3) sJYM = "9"; 
else if (iJYM == 4) sJYM = "8"; 
else if (iJYM == 5) sJYM = "7"; 
else if (iJYM == 6) sJYM = "6"; 
else if (iJYM == 7) sJYM = "5"; 
else if (iJYM == 8) sJYM = "4"; 
else if (iJYM == 9) sJYM = "3"; 
else if (iJYM == 10) sJYM = "2"; 
var cCheck = v_card.charAt(17).toLowerCase(); 
if (cCheck != sJYM) { 
return false; 
} 
} 
return true; 
} 
</script>

来自 http://www.jb51.net/article/25974.htm

普通分类: