JS/JQUERY(9)——JS表单校验(电话,邮编,手机号等)

function isMail(obj,str,allowNull) {  
    var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
        //document.getElementById('doing').style.visibility='hidden';   
        alert(str+" 不是合法电子邮件格式!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
//非法字符校验,以英文字母开头其后只能包含英文字母、数字及"_"  
function isEN(obj,str,allowNull) {  
    var pattern = /^([a-zA-Z])+([a-zA-Z0-9_]*)+$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
        //document.getElementById('doing').style.visibility='hidden';    
        alert(str+" 必须以英文字母开头其后只能包含英文字母、数字及'_'");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
function isNotNull (obj,str,allowNull){  
  if (isNull(obj) && !allowNull){  
 // document.getElementById('doing').style.visibility='hidden';    
    alert(str+" 不能为空!");  
    obj.focus();  
    return false;  
  }  
  else return true;  
}  
  
function isNotNull1 (obj,str,allowNull){  
  if ((isNull(obj)||trim(obj.value)=="null") && !allowNull){  
    //document.getElementById('doing').style.visibility='hidden';    
    alert(str+" 不能为空!");  
    obj.focus();  
    return false;  
  }  
  else return true;  
}  
  
function isNull(obj){  
    if(!obj.value || trim(obj.value)=="") return true;  
    else return false;  
}  
//  
  
function isNumber(obj,str,allowNull) {  
    var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';    
        alert(str+" 不是数字格式!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
function isInteger(obj,str,allowNull) {  
    var pattern = /^-*\d+$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';    
        alert(str+" 不是整数格式!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
function isIntegerInfo(obj,str,allowNull) {  
    var pattern = /^-*\d+$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
        //document.getElementById('doing').style.visibility='hidden';    
        alert(str);  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
  
function isDate(obj,str,allowNull) {  
    var pattern = /^[1-9]\d{3}[/|-]((0[1-9])|(1(0|1|2))|([1-9]))[/|-](([0-2][1-9])|([1-2][0-9])|(3(0|1))|([1-9]))$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
        //document.getElementById('doing').style.visibility='hidden';    
        alert(str+" 不是日期格式!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
  
function verifyPassword(obj1, obj2) {  
  if (obj1.value != obj2.value) {  
     // document.getElementById('doing').style.visibility='hidden';    
      alert("输入的密码不一致!");  
    return false;  
  }  
  return true;  
}  
  
function checkMobile(obj,str,allowNull){  
      var pattern=/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';       
        alert(str+" 格式不对!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
/**********验证身份证号码的有效性***********/  
function isIdCard(obj,str,allowNull){  
    var pattern = /^\d{15}(\d{2}[A-Za-z0-9])?$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
     //   document.getElementById('doing').style.visibility='hidden';     
        alert(str+" 不是正确的身份证号码!");    
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证手机号码的有效性***********/  
function isMobile(obj,str,allowNull){  
    var pattern = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
     //  document.getElementById('doing').style.visibility='hidden';     
        alert(str+" 不是正确的手机号码!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证电话号码的有效性***********/  
function isTel(obj,str,allowNull){  
    var pattern = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';       
        alert(str+" 不是正确的电话号码!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证email的有效性***********/  
function isEmail(obj,str,allowNull){  
    var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
      // document.getElementById('doing').style.visibility='hidden';      
        alert(str+" 不是正确的E_mail!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证IP地址的有效性***********/  
function isIp(obj,str,allowNull){  
    if(!isNotNull(obj,str,allowNull)) return false;  
     var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};  
     var re=obj.value.split(".");  
     //return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false;  
        if((re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false &&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';           
        alert(str+" 不是正确的IP!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证只能为数字或字母***********/  
function isNumOrE(obj,str,allowNull){  
    var pattern = new RegExp("^[a-zA-Z0-9]+{1}quot;);  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';       
        alert(str+"只能数字或者字母!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
  
/**********验证邮政编码的有效性***********/  
function isZip(obj,str,allowNull){  
    var pattern =  /^[1-9]\d{5}$/;  
    if(!isNotNull(obj,str,allowNull)) return false;  
    if(!(pattern.test(obj.value))&&!isNull(obj)){  
    //  document.getElementById('doing').style.visibility='hidden';       
        alert(str+" 不是正确的邮政编码!");  
        obj.focus();  
        return false;  
    }  
    else return true;  
}  
/**********去除左右空格***********/  
function trim(str){  
    return str.replace(/(^\s*)|(\s*$)/g, "");  
}  
/* 
function isNumber(obj) { 
    var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/; 
    var oldValue = obj.value; 
    if(!(pattern.test(obj.value))){ 
        document.getElementById('doing').style.visibility='hidden';   
        alert("不是数字格式!"); 
        obj.value = oldValue; 
        obj.focus(); 
        return false; 
    } 
    else return true; 
}*/  
function strToDate(str)  
{  
  var arys= new Array();  
  arys=str.split('-');  
  var newDate=new Date(arys[0],arys[1],arys[2]);   
  return newDate;  
}  

 

posted @ 2015-04-15 09:36  xu_shuyi  阅读(262)  评论(0)    收藏  举报