JQuery正則驗證


首先,引入一個jquery文件
<script src="assets/js/jquery-1.7.2.min.js"></script>

<script type="text/javascript">
function register2(){
    
    if($("#PHONE_NUMBER").val()==""){
        alert("驗證碼不可為空");
        return false;
    }
    if($("#USERNAME").val()==""){
        alert("用戶名不可為空");
        return false;
    }
    if($("#PASSWORD").val()==""){
        alert("密碼不可為空");
        return false;
    }
    if($("#REPASSWORD").val()==""){
        alert("確認密碼不可為空");
        return false;
    }
    var s=$("#PASSWORD").val();
    var a=$("#REPASSWORD").val();
    if(s!=a){
        alert("兩次密碼不一致");
        return false;
    }
    
    //校驗密碼:只能輸入6-12個字母、數字、下划線 
    var patrn = /^(w){6,12}$/;
    if(!patrn.test($("#PASSWORD").val())) 
    { 
        alert('只能輸入6-12個字母、數字、下划線 !'); 
        return false; 
    } 
    /*用戶名校驗:只能輸入4-20個以字母開頭、可帶數字、“_”、“.”的字串  */
     var name = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,19}$/;
     if(!name.test($("#USERNAME").val())) 
        { 
            alert('用戶名只能輸入4-20個以字母開頭、可帶數字、“_”、“.”的字串!'); 
            return false; 
        } 
     
    $("#FORM2").submit();
    $("#zhongxin").hide();
    $("#zhongxin2").show();
    
}

</script>
 
         

觸發方法的事件:

<input type="button" onclick="register2();" class="am-btn am-btn-primary  am-btn-block tpl-btn-bg-color-success  tpl-login-btn" value="下一步" />


驗證滑塊是否划過
<script type="text/javascript">
function register1(){
    
    /*  驗證130-139,150-159,180-189號碼段的手機號碼 */
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
    if(!myreg.test($("#PHONE").val())) 
    { 
        alert('請輸入有效的手機號碼!'); 
        return false; 
    } 
    
    if($("#PHONE").val()==""){
        alert("手機號碼不為空");
        $("#PHONE").focus();
        return false;
    }
    
    if($('#drag').css("color")!="rgb(255, 255, 255)"){
        alert("請拖動滑塊");
        return false;
    }
    $("#FORM1").submit();
    $("#zhongxin").hide();
    $("#zhongxin2").show();
    
}

</script>
 
         

 











 
 
         

 






function checkIshanzi(s) {
//var patrn = /^[\u2E80-\u9FFF]$/; //Unicode編碼中的漢字范圍 /[^\x00-\x80]/ var patrn = /[^\x00-\x80]/; if (!patrn.exec(s)) return false return true } //校驗登錄名:只能輸入4-20個以字母開頭、可帶數字、“_”、“.”的字串 function checkIsRegisterUserName(s) { var patrn = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){3,19}$/; if (!patrn.exec(s)) return false return true } //校驗用戶姓名:只能輸入4-30個以字母開頭的字串 function checkIsTrueName(s) { var patrn = /^[a-zA-Z]{4,30}$/; if (!patrn.exec(s)) return false return true } //校驗密碼:只能輸入6-20個字母、數字、下划線 function checkIsPasswd(s) { var patrn = /^(w){6,20}$/; if (!patrn.exec(s)) return false return true } //校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-” function checkIsTel(s) { var patrn = /^[+]{0,1}(d){1,4}[ ]?([-]?((d)|[ ]){1,12})+$/; if (!patrn.exec(s)) return false return true } //校驗手機號碼 function checkIsMobil(s) { var patrn = /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/; if (!patrn.exec(s)) return false return true } //校驗郵政編碼 function checkIsPostalCode(s) { var patrn = /^[a-zA-Z0-9 ]{3,12}$/; if (!patrn.exec(s)) return false return true } //校驗是否IP地址 function checkIsIP(s) { var patrn = /^[0-9.]{1,20}$/; if (!patrn.exec(s)) return false return true } //校驗EMail function checkIsEMail(s) { //var regex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/; var patrn = /^([0-9A-Za-z\-_\.]+)@([0-9A-Za-z]+\.[A-Za-z]{2,3}(\.[A-Za-z]{2})?)$/g; if (!patrn.exec(s)) return false return true } //驗證判斷類型 function check(axd,lbm,int) { var s = $("#" + axd).val(); switch (int) { case 0: if (s == "") { $("#textt").text(lbm + "不能為空"); $(".tip").fadeIn(200); return false } break; case 1: if (!checkIsDigit(s)) { $("#textt").text(lbm + "只能全由數字組成,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 2: if (checkIsDigit(s)) { $("#textt").text(lbm + "不能全由數字組成,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 3: if (!checkIsRegisterUserName(s)) { $("#textt").text(lbm + "只能輸入4-20個以字母開頭、可帶數字、“_”、“.”的字串,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 4: if (!checkIsTrueName(s)) { $("#textt").text(lbm + "只能輸入4-30個以字母開頭的字串,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 5: if (!checkIsPasswd(s)) { $("#textt").text(lbm + "只能輸入6-20個字母、數字、下划線,請重新輸入! "); $(".tip").fadeIn(200); return false } break; case 6: if (!checkIsTel(s)) { $("#textt").text(lbm + "校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-”,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 7: if (!checkIsMobil(s)) { $("#textt").text(lbm + "不正確,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 8: if (!checkIsPostalCode(s)) { $("#textt").text(lbm + "不正確,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 9: if (!checkIsIP(s)) { $("#textt").text(lbm + "不正確,請重新輸入!"); $(".tip").fadeIn(200); return false } break; case 10: if (!checkIsEMail(s)) { $("#textt").text(lbm + "不正確,請重新輸入!"); $(".tip").fadeIn(200); return false } case 11: if (!checkIshanzi(s)) { $("#textt").text(lbm + "只能全由漢字組成,請重新輸入!"); $(".tip").fadeIn(200); return false } break; } return true } function checkrid(axd, lbm, int) { var s = $("input[name=" + axd + "]:checked").val(); switch (int) { case 0: if (s == "") { $("#textt").text(lbm + "不能為空"); $(".tip").fadeIn(200); } break; } } //驗證判斷 function checked(asd) { for (var i = 0; i < asd.length; i++) { var axd = asd[i][0]; var lbm = asd[i][1]; var int = asd[i][2]; if (!check(axd, lbm, int)) { return false; break; } } return true; } function ShowName(i) { if (i == 1) { $("#textt").text("修改成功"); $(".tip").fadeIn(200); } else if (i == 0) { $("#textt").text("添加成功"); $(".tip").fadeIn(200); }if (i == 2) { $("#textt").text("刪除成功"); $(".tip").fadeIn(200); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM