JS和正则表达式验证手机号码、邮箱等

时间:2022-10-04 22:55:26

<-------------------------------------验证手机号码--------------------------------------------->

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 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("两次输入的密码不相同!");
                                }

}

就是在使用JS文件的时候,那么<script>里面就不能再添加功能函数了,否则在调用的时候会失灵,也就是说JS文件的优先级比较高。