2013-12-21 55 views
-3

我有這樣的代碼財產「密碼強度」,並獲得在第一行JavaScript錯誤:無法設置的不確定

錯誤是「無法設置屬性‘密碼強度’的未定義」

代碼:

window.myProject.passwordStrength = function ($, window, document) { 
var desc = new Array(); 
desc[0] = "Very Weak"; 
desc[1] = "Weak"; 
desc[2] = "Better"; 
desc[3] = "Medium"; 
desc[4] = "Strong"; 
desc[5] = "Very Strong"; 
return { 
    maxScore: 5, 
    allowedScore: 4, 
    getStrengthDescription: function(score){ 
     return desc[score]; 
    }, 
    getStrength: function(password){ 


     var score = 0; 

     //if password bigger than 6 give 1 point 
     if (password.length > 6) score++; 

     //if password has both lower and uppercase characters give 1 point 
     if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) score++; 

     //if password has at least one number give 1 point 
     if (password.match(/\d+/)) score++; 

     //if password has at least one special caracther give 1 point 
     if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) score++; 

     //if password bigger than 12 give an other 1 point 
     if (password.length > 12) score++; 
     return score; 
    } 
}; 
}(jQuery, window, document); 

我在我的頁面調用此爲:

<script> 
    $(function() { 
     $(document).ready(function() { 
      var passwordStrength = window.myProject.passwordStrength(); 
      $('#Password').keyup(function() { 
       var strength = passwordStrength.getStrength($(this).val()); 
} 
</script> 
+3

是什麼'window.myProject'? – Krishna

+1

究竟是什麼問題? –

+0

錯誤消息應該是不言自明的。 'window.myProject'是未定義的(即它不存在)。 – JJJ

回答

4

你必須定義對象你定義一個新的屬性到該對象

window.myProject = {}; 
window.myProject.passwordStrength = function ($, window, document) { 

FIDDLE

+0

現在它給出錯誤屬性'passwordStrength'的對象#不是一個函數 – Sami

+0

這是因爲它不是一個函數。你有一個立即執行的IIFE,並返回一個對象,所以'window.myProject.passwordStrength'的值就是你返回的對象,而不是一個函數。 – adeneo

+1

@Sami - 下面是你如何做到 - > http://jsfiddle.net/vYJVe/ – adeneo

1

之前,我會建議你這樣做,否則。更簡單的&清潔劑。

的jsfiddle:http://jsfiddle.net/VmAHF/

JS:

(function ($) { 
    var desc = [ 'Very Weak', 'Weak', 'Better', 'Medium', 'Strong', 'Very Strong' ]; 

    var maxScore = 5, 
     allowedScore = 4; 

    $.fn.getStrength = function (password) { 
     var password = $(this).val(), 
      score = 0; 

     //if password bigger than 6 give 1 point 
     if (password.length > 6) score++; 

     //if password has both lower and uppercase characters give 1 point 
     if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) score++; 

     //if password has at least one number give 1 point 
     if (password.match(/\d+/)) score++; 

     //if password has at least one special caracther give 1 point 
     if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) score++; 

     //if password bigger than 12 give an other 1 point 
     if (password.length > 12) score++; 
     return score; 
    }; 

    $.fn.getStrengthDescription = function() { 
     return desc[$(this).getStrength()]; 
    }; 
})(jQuery); 

$(document).ready(function() { 
    $('#password').keyup(function() { 
     $('#strength').text($(this).getStrength()); 
     $('#strength-description').text($(this).getStrengthDescription()); 
    }); 
}); 
+0

謝謝,這也是一個很好的解決方案。 – Sami