By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

user and Password validation script

P: 3
I'm writing a password validation script and I found the script below, it works fine but I want it to validate apassword with exact 8 characters tried different solutions but can't get it work, can you help please?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3.   function checkForm(form)
  4.   {
  5.     if(form.username.value == "") {
  6.       alert("Error: Username cannot be blank!");
  7.       form.username.focus();
  8.       return false;
  9.     }
  10.     re = /^\w+$/;
  11.     if(!re.test(form.username.value)) {
  12.       alert("Error: Username must contain only letters, numbers and underscores!");
  13.       form.username.focus();
  14.       return false;
  15.     }
  16.  
  17.     if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
  18.          if(form.pwd1.value.length < 6){
  19.         alert("Error: Password must contain at least six characters!");
  20.         form.pwd1.focus();
  21.         return false;
  22.       }
  23.       if(form.pwd1.value == form.username.value) {
  24.         alert("Error: Password must be different from Username!");
  25.         form.pwd1.focus();
  26.         return false;
  27.       }
  28.       re = /[0-9]/;
  29.       if(!re.test(form.pwd1.value)) {
  30.         alert("Error: password must contain at least one number (0-9)!");
  31.         form.pwd1.focus();
  32.         return false;
  33.       }
  34.       re = /[a-z]/;
  35.       if(!re.test(form.pwd1.value)) {
  36.         alert("Error: password must contain at least one lowercase letter (a-z)!");
  37.         form.pwd1.focus();
  38.         return false;
  39.       }
  40.       re = /[A-Z]/;
  41.       if(!re.test(form.pwd1.value)) {
  42.         alert("Error: password must contain at least one uppercase letter (A-Z)!");
  43.         form.pwd1.focus();
  44.         return false;
  45.       }
  46.     } else {
  47.       alert("Error: Please check that you've entered and confirmed your password!");
  48.       form.pwd1.focus();
  49.       return false;
  50.     }
  51.  
  52.     alert("You entered a valid password: " + form.pwd1.value);
  53.     return true;
  54.   }
  55.  
  56. </script> <form ... onsubmit="return checkForm(this);"> <p>Username: <input type="text" name="username"></p> <p>Password: <input type="password" name="pwd1"></p> <p>Confirm Password: <input type="password" name="pwd2"></p> <p><input type="submit"></p> </form>
Aug 15 '14 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,347
I see in your code that you check for at least 6 characters, I'm not sure what trouble you are having checking for 8.
Aug 15 '14 #2

P: 3
Thank you Rabbit for your prompt reply to my query.
my trouble is that I want to validate exact number of characters, not less, no more than 6 but exactly six characters.
Aug 16 '14 #3

Exequiel
100+
P: 287
you can do it this way.
Expand|Select|Wrap|Line Numbers
  1. if(form.pwd1.value.length == 6)
  2.  
Aug 16 '14 #4

P: 3
Thanks Exequiel, I'm On my way! Way to work and will let you know after trying.
Thanks
Aug 18 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.