Jquery强密码验证

时间:2018-01-30 22:14:06

标签: javascript jquery

我很抱歉,但我对javascript和Jquery都很陌生! 我一直在努力从一个插件中制作这段代码,我只使用强密码。

满足最小长度要求,匹配字段并检查字段是否为空;但我不能把它纳入A-Z,a-z,1-0等......这是我最好的尝试不起作用

非常感谢任何帮助!

function strongpass_submit(url) {   

    jQuery("#strong-error", jQuery("#action")).hide();
    var witherror = false;
    var passval = jQuery("#strong-pw", jQuery("#action")).val();
    var okval = jQuery("#strong-pw-confirm", jQuery("#action")).val();
    if (passVal == '') {
            jQuery("#strong-error", jQuery("#action")).html('Please enter a password.').fadeIn();
            return false;

//THIS IS THE PART I'M HAVING TROUBLE WITH
    } else if (okval.search(/^([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6})*([,;][\s]*([A-Z0-9.%+-]+@@[A-Z0-9.-]+.[A-Z]{2,6}))*$/i)) {
        jQuery("#strong-error", jQuery("#action")).html('Password must be strong.').fadeIn();
        return false;
// UNTIL HERE

    } else if (okval.length < 8) {
        jQuery("#strong-error", jQuery("#action")).html('Password must be at least 8 characters.').fadeIn();
        return false;

    } else if (okval == '') {
        jQuery("#strong-error", jQuery("#action")).html('Please re-enter your password.').fadeIn();
        return false;
    } else if (passval != okval ) {
        jQuery("#pass-error", jQuery("#action")).html('Passwords do not match.').fadeIn();
        return false;
    }
    jQuery('#action #maindiv').fadeOut('fast', function(){
        jQuery('#popout').show();
        jQuery.ajax({
            url: url,
            dataType: 'json',
            type: 'post',
            data: jQuery('#action form').serialize(),
            success: function(data){
                if(data.confirm == 'success'){
                    jQuery('#cboxLoadingGraphic').fadeOut('fast', function(){
                        jQuery('#action #second').fadeIn();
                    });
                }
                else {
                    jQuery('#cboxLoadingGraphic').fadeOut('fast', function(){
                        jQuery('#action #openerror').fadeIn();
                    });
                }
            }
        });
    });
};

2 个答案:

答案 0 :(得分:1)

<style>
   .weak{
   color:red;
   font-size: 17px;
   }
   .good{
   color: #17da17;
   font-size: 17px;
   }
   .strong{
   color: #339333;
   font-size: 17px;
   }
</style>
<form id="password-check">
   <label>Password : </label>
   <input name="pass" id="pass" type="password"/>
   <span id="message"></span>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
   $(document).ready(function()
   {
   $('#pass').keyup(function()
   {
   $('#message').html(checkStrength($('#pass').val()))
   }) 
   function checkStrength(password)
   {
    var useremail="testing@gmail.com";
    var username="testing";
   
    var strength = 0
    if (password.length < 6) { 
        $('#message').removeClass();
        $('#message').addClass('weak');
        return 'Too short!';
    }
    if ((username.indexOf(password) > -1) || (password.indexOf(username)> -1))
    {
        $('#message').removeClass();
        $('#message').addClass('weak');
        return "Password must be different from user name!"; 
    }
    if ((useremail.indexOf(password) > -1) || (password.indexOf(useremail)> -1))
    {
        $('#message').removeClass();
        $('#message').addClass('weak');
        return "Password must be different from user email!"; 
    }
    if (password.length > 7) strength += 1
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  strength += 1 
    if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1
    if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
    if (strength < 2 )
    {
        $('#message').removeClass();
        $('#message').addClass('weak');
        return 'Weak!';   
    }
    else if (strength == 2 )
    {
        $('#message').removeClass();
        $('#message').addClass('good');
        return 'Good!';  
    }
    else
    {
        $('#message').removeClass();
        $('#message').addClass('strong');
        return 'Strong!';
    }
   }
   });
</script>

答案 1 :(得分:0)

看来这些有问题的行中的正则表达式是错误的 为了验证具有此类要求的输入,您可以使用此代码,并使用多个正则表达式:

&#13;
&#13;
var okval = "Abcd123!";
var print = (/[A-Z]/.test(okval) && /[0-9]/.test(okval) && /[^A-Za-z0-9]/.test(okval) && okval.length>7);
console.log(print);
&#13;
&#13;
&#13;