JavaScript常用页面正则表达式!

<-------------------------------------验证手机号码--------------------------------->
function checktelephone() {
         var cellPhone=document.getElementByIdx_x_x_x_x_x("telephone");
         var RegCellPhone = /^([0-9]{11})?$/;
         falg=cellPhone.value.search(RegCellPhone);
         if (falg==-1){
             alert("手机号不合法!");
          }
} 
 
function checkMobile(){
    var sMobile = document.mobileform.mobile.value
    if(!(/^1[3|5][0-9]\d{4,8}$/.test(sMobile))){
        alert("不是完整的11位手机号或者正确的手机号前七位");
        document.mobileform.mobile.focus();
        return false;
    }
}

<------------------匹配国内座机号码(0511-4405222 或 021-87888822) ------------->
function istell(str){ 
    var result=str.match(/\d{3}-\d{8}|\d{4}-\d{7}/); 
    if(result==null)
        return false; 
          return true; 
} 

<-------------------------------------验证邮箱------------------------------------>

function checkemail(){            
     var temp = document.getElementByIdx_x_x_x_x("email");
     var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
     if(temp.value!=""){
     if(!myreg.test(temp.value)){
         alert("email格式不正确,请重新输入!");
         email.focus();
         return false;
     }
}

 

<-------------------------------------验证用户名的长度-------------------------->

function checknamelength(){
               var aaa = document.getElementByIdx_x_x_x("username");//用户名
               if(aaa.value.length<=3||aaa.value.length>=8) {
                   alert("用户名长度必须大于3小于8");
               } 
}

 

<-------------------------------------验证密码的长度----------------------------->

 function checkpasswordlength(){
        var bbb = document.getElementByIdx_x_x_x("password");
        if(bbb.value.length<=6||bbb.value.length>=12){
           alert("密码长度必须大于6小于12");
         }
}   

 

<-------------------------------------验证两次密码的正确性----------------------->

 function checkpassword(){
        var password1=document.getElementByIdx_x_x_x("password");
        var password2=document.getElementByIdx_x_x_x("password2");
         if (password2.value!==password1.value){
                 alert("两次输入的密码不相同!");
           }
}  

<------------------------------------去除表单元素两端空格------------------------>
function trim(str){ 
      return str.replace(/(^\s*)|(\s*$)/g, ""); 
} 


<-------------------------------匹配中国邮政编码(6位)---------------------------->
function ispostcode(str){
        var result=str.match(/[1-9]\d{5}(?!\d)/); 
        if(result==null)
          return false;
             return true; 
} 

你可能感兴趣的:(JavaScript,正则表达式)