JS密碼校驗規則前台驗證(不能連續字符(如123、abc)連續3位或3位以上)(不能相同字符(如111、aaa)連續3位或3位以上)


  • 密碼必須為8到16位且必須包含數字和字母
  • 密碼必須包含特殊字符【_&#%】
  • 不能連續字符(如123、abc)連續3位或3位以上
  • 不能相同字符(如111、aaa)連續3位或3位以上
/**
 * 外層密碼修改
 */
(function(){
    var modifyPassword = {};//------這個可以自己去修改--不同的頁面取名不一樣
    /**
     * 進入頁面后需要初始化的事件
     */
    modifyPassword.initEvent = function(){
        //獲取傳遞的參數
        var userId = getQueryString("user_id");
        $("#user_id").val(userId);
    };
    
    do_submit = function(){
        //序列話數據
        var param = $('#modifPwd').serializeObject();
        
        var user_id =($("#user_id").val()).trim();
        var login_passwd_old = ($("#login_passwd_old").val()).trim();
        var login_passwd_new = ($("#login_passwd_new").val()).trim();
        var login_passwd_new_re = ($("#login_passwd_new_re").val()).trim();
        //密碼必須包含數字和字母
        //密碼長度8到16位
        var regex = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}/;
        if(!regex.test(login_passwd_new)){
            alert("您的工號為弱口令密碼,請修改密碼后登錄");
            return false;
        }
        //密碼必須包含特殊字符 _&#%
        if(!(login_passwd_new.indexOf("_")!=-1||login_passwd_new.indexOf("&")!=-1||login_passwd_new.indexOf("#")!=-1||login_passwd_new.indexOf("%")!=-1)){
            alert("您的工號為弱口令密碼,請修改密碼后登錄");
            return false;
        }
        //不能連續字符(如123、abc)連續3位或3位以上
        if(!LxStr(login_passwd_new)){
            $.messager.alert("提示", "您的工號為弱口令密碼,請修改密碼后登錄");
            return false;
        }
        //不能相同字符(如111、aaa)連續3位或3位以上
        var re = /(\w)*(\w)\2{2}(\w)*/g;
        if(re.test(login_passwd_new)){
            alert("您的工號為弱口令密碼,請修改密碼后登錄");
            return false;
        }
        if(login_passwd_old==login_passwd_new){
            alert("新密碼與舊密碼重復");
            return false;
        }
        if($.trim(login_passwd_new) != $.trim(login_passwd_new_re)){
            alert("新密碼與重復密碼不一致");
            return;
        }
        if(user_id==""){
            return;
        }
        if(login_passwd_old==""||login_passwd_new==""){
            alert("提示", "每一項都是必須填寫的喲!");
            return;
        }; 
        param.current_passwd = $.md5(param.login_passwd_old);
        param.new_passwd = $.md5(param.login_passwd_new);
        param.user = user_id
        $.post(ctx+'/updatePassword.action',{user_id:user_id,current_passwd:$.md5(login_passwd_old),new_passwd:$.md5(login_passwd_new)},function(data){
            if(data.code == '0000'){
                alert("修改成功請重新登陸");
                window.location = "/lms-base/login.jsp"
            }else{
                alert("修改失敗");
            }
        },'json');
    }
  //不能連續字符(如123、abc)連續3位或3位以上
    LxStr = function(str){
        var arr = str.split('');
        var flag = true;
        for (var i = 1; i < arr.length-1; i++) {
            var firstIndex = arr[i-1].charCodeAt();
            var secondIndex = arr[i].charCodeAt();
            var thirdIndex = arr[i+1].charCodeAt();
            thirdIndex - secondIndex == 1;
            secondIndex - firstIndex==1;
            if((thirdIndex - secondIndex == 1)&&(secondIndex - firstIndex==1)){
                flag =  false;
            }
        }
        if(!flag){
            $("#message_").text("您的工號為弱口令密碼,請修改密碼后登錄!");
            return flag;
        }
        return flag;
    }
    getQueryString = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if ( r != null ){
            return decodeURI(r[2]);
        }else{
            return null;
        }
    }
    $.fn.serializeObject = function()
    {
       var o = {};
       var a = this.serializeArray();
       $.each(a, function() {
           if (o[this.name]) {
               if (!o[this.name].push) {
                   o[this.name] = [o[this.name]];
               }
               o[this.name].push(this.value || '');
           } else {
               o[this.name] = this.value || '';
           }
       });
       return o;
    };
    /**
     * 此方法挪到最后調用
     */
    $(document).ready(function(){
        modifyPassword.initEvent();
    });
})();

 


免責聲明!

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



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