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();
        });
    })();

    主要看里面的判断的内容  其他的代码可以忽略   判断密码校验规则的几个判断很不错 大家乐意收藏 毕竟任何一个 软件都需要登录。

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值