Posted By

fuelishways on 06/19/14


Tagged

jquery


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Beppoi
JeffSFO


test password strength


 / Published in: jQuery
 

When building forms, it’s a very good practice to provide verifications on the front-end first so the visitor do not have to submit the form endlessly to correct problems. This code snippet is using regular expressions to test if a password is strong enough. Of course, don’t forget to validate your forms on the server side as well! http://www.catswhocode.com/blog/useful-jquery-code-snippets

  1. $('#pass').keyup(function(e) {
  2. var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
  3. var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
  4. var enoughRegex = new RegExp("(?=.{6,}).*", "g");
  5. if (false == enoughRegex.test($(this).val())) {
  6. $('#passstrength').html('More Characters');
  7. } else if (strongRegex.test($(this).val())) {
  8. $('#passstrength').className = 'ok';
  9. $('#passstrength').html('Strong!');
  10. } else if (mediumRegex.test($(this).val())) {
  11. $('#passstrength').className = 'alert';
  12. $('#passstrength').html('Medium!');
  13. } else {
  14. $('#passstrength').className = 'error';
  15. $('#passstrength').html('Weak!');
  16. }
  17. return true;
  18. });

Report this snippet  

You need to login to post a comment.