Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,274 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,157 people online right now. Registration is fast and FREE... Join Now!





Password Validity Tester

ORIGINALLY WRITTEN BY Sani, A. I.

Submitted By: born2c0de
Actions:
Rating:
Views: 18,111

Language: JavaScript

Last Modified: February 27, 2005

Snippet


  1. <html>
  2.  
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  5. <title>Test Password</title>
  6. <SCRIPT LANGUAGE="JavaScript">
  7.  
  8. function validatePwd(fieldname) {
  9. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  10. //Copyright April 2004 Sani, A. I. (MCSE, MCSA, CCNA)
  11. //sanijean@yahoo.co.uk
  12. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13.  
  14.  
  15. //Initialise variables
  16. var errorMsg = "";
  17. var space  = " ";
  18. fieldname   = document.myform.password;
  19. fieldvalue  = fieldname.value;
  20. fieldlength = fieldvalue.length;
  21.  
  22. //It must not contain a space
  23. if (fieldvalue.indexOf(space) > -1) {
  24.      errorMsg += "\nPasswords cannot include a space.\n";
  25. }     
  26.  
  27. //It must contain at least one number character
  28. if (!(fieldvalue.match(/\d/))) {
  29.      errorMsg += "\nStrong passwords must include at least one number.\n";
  30. }
  31. //It must start with at least one letter     
  32. if (!(fieldvalue.match(/^[a-zA-Z]+/))) {
  33.      errorMsg += "\nStrong passwords must start with at least one letter.\n";
  34. }
  35. //It must contain at least one upper case character     
  36. if (!(fieldvalue.match(/[A-Z]/))) {
  37.      errorMsg += "\nStrong passwords must include at least one uppercase letter.\n";
  38. }
  39. //It must contain at least one lower case character
  40. if (!(fieldvalue.match(/[a-z]/))) {
  41.      errorMsg += "\nStrong passwords must include one or more lowercase letters.\n";
  42. }
  43. //It must contain at least one special character
  44. if (!(fieldvalue.match(/\W+/))) {
  45.      errorMsg += "\nStrong passwords must include at least one special character - #,@,%,!\n";
  46. }
  47. //It must be at least 7 characters long.
  48. if (!(fieldlength >= 7)) {
  49.      errorMsg += "\nStrong passwords must be at least 7 characters long.\n";
  50. }
  51. //If there is aproblem with the form then display an error
  52.      if (errorMsg != ""){
  53.           msg = "______________________________________________________\n\n";
  54.           msg += "Please correct the problem(s) with your trial password test it again.\n";
  55.           msg += "______________________________________________________\n";
  56.           errorMsg += alert(msg + errorMsg + "\n\n");
  57.           fieldname.focus();
  58.           return false;
  59.      }
  60.      
  61.      return true;
  62. }
  63.  
  64. </script>
  65.  
  66. </head>
  67.  
  68. <body>
  69. <form name="myform" onSubmit="return validatePwd('password')">
  70. <p>
  71. <INPUT NAME="password" SIZE=30>
  72. </p>
  73. <P>
  74. <INPUT TYPE="submit" value="Test">
  75. </body>
  76.  
  77. </html>

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month