Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There were several coding errors and logic errors. I would suggest this rewrite that makes the following fixes/improvements:</p> <ol> <li>Get the actual password value and use its length instead of using the length of the password DOM element.</li> <li>Handle the case of the length between 4 and 7 (a case your logic skipped)</li> <li>Sequence the regex tests in the right order so it actually finds the proper score</li> <li>Corrected the punctuation regex</li> <li>Fix else logic problem</li> <li>Put the colors into a parallel array.</li> <li>Declare the strength strings in a more efficient/compact way.</li> <li>Give your score a default value so it is guaranteed to have a value.</li> <li>Don't repeat regex tests more than once</li> <li>Use <code>regex.test</code> because it's more efficient than <code>regex.match</code> when all you want to know is if it matched</li> <li>Pull the desired color value from the array rather than an <code>if</code> statement for every score</li> <li>Only fetch the <code>spnPwd</code> DOM element once</li> <li>Changed the numeric regex to <code>\d</code></li> <li>Use braces on all if/else statements for safety</li> </ol> <p>And the suggested code:</p> <pre><code>function pwd_Validation() { var strengths = ["Blank", "Very Weak", "Weak", "Medium", "Strong", "Very Strong"]; var colors = ["#FF0000", "#FF0000", "#FFCC00", "#FFCC00", "#19D119", "#006600"]; var score = 0; var regLower = /[a-z]/, regUpper = /[A-Z]/, regNumber = /\d/, regPunctuation = /[.,!@#$%^&amp;*?_~\-£()]/; var password = document.getElementById('pwd').value; if (!password) { score = 0; } else if (password.length &lt; 2) { score = 1; } else if (password.length &lt; 4) { score = 2; } else if (password.length &lt;= 7) { score = 3; } else { // length is &gt;= 8 in here if (regLower.test(password) &amp;&amp; regUpper.test(password) &amp;&amp; regNumber.test(password)) { // if it also has punctuation, then it gets a 5, otherwise just a 4 if (regPunctuation.test(password)) { score = 5; } else { score = 4; } } else { // if doesn't have upper, lower and numbers, then it gets a 3 score = 3; } } var spanPwd = document.getElementById('spnPwd'); spanPwd.innerHTML = strengths[score]; spanPwd.style.color = colors[score]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload