簡體   English   中英

密碼強度腳本失敗

[英]Password strength script failed

我使用一個小腳本以題字形式檢查密碼強度,這是腳本的相關部分:

    $('#password').keyup(function(){

    var password = $(this).val();
    password = $.trim(password);

    var lettre_min = /[a-z]/;
    var lettre_maj =/[A-Z]/;
    var nombre = /[0-9]/;
    var symbole = /[\`\!\"\?\$\%\^\&\*\(\)\_\-\+\=\{\[\}\]\:\;\@\'\~\#\|\\\<\,\>\.\/]/;

    if(password.length != 0)
    {   
        //password moin de 8 caractères
        if(password.length <8)
        {
            $('.bar').animate({width:'50px',height:'5px'},200).show();
            $('.bar').css('background-color','red');
            $('.error').text('Too short').show();
        }else

        //password faible
        if((password.match(lettre_min)) && password.length <12)
        {
            $('.bar').animate({width:'75px',height:'5px'},200).show();
            $('.bar').css('background-color','red');
            $('.error').text('Weak').show();
        }else

        //password moyen
        //1 type
        if((password.match(lettre_min)) && (password.match(nombre)) && password.length <12)
        {
            $('.bar').animate({width:'100px',height:'5px'},200).show();
            $('.bar').css('background-color','orange');
            $('.error').text('Average').show();
        }else ...

問題是:如果我在表單輸入中輸入字母(lettre_min)和數字(nombre),當他告訴我該密碼是平均密碼時,他告訴我密碼很弱。 他完全無視第二個條件。

我不知道這是怎么回事= /

PS:很抱歉,如果另一個問題已經有了答案,但是我什至不知道問題出在哪里,所以我不知道要搜索= /

實際上,我傾向於認為沒有評估第三個條件,因為它是第二個條件的較大情況,請考慮將第二個If切換為第三個條件,它應該可以工作。

第二個if可以包含小寫字母,但也可以包含數字,因為match()會評估它是否包含這種東西,因此在執行第二個執行之前,第三個wich也將檢查是否有數字,如果沒有,則僅檢查字母。

快速解決方案:

//password moyen
        //1 type
        if((password.match(lettre_min)) && (password.match(nombre)) && password.length <12)
        {
            $('.bar').animate({width:'100px',height:'5px'},200).show();
            $('.bar').css('background-color','orange');
            $('.error').text('Average').show();
        } else if((password.match(lettre_min)) && password.length <12)
        {
            $('.bar').animate({width:'75px',height:'5px'},200).show();
            $('.bar').css('background-color','red');
            $('.error').text('Weak').show();
        }

如果if((password.match(lettre_min)) && (password.match(nombre)) && password.length <12)為true, if((password.match(lettre_min)) && password.length <12)也為true。 。

您的訂單條件檢查密碼的強度是錯誤的。 要解決這個問題:

if((password.match(lettre_min)) && password.length <12){
   if(password.match(nombre)){
     //average
   }else{
     //weak
   }
}else if(password.length < 8){
   //too short
}else{
   //it must be strong
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM