Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 27th, 2009, 12:27 AM
Friend of Wrox
Points: 6,378, Level: 34
Points: 6,378, Level: 34 Points: 6,378, Level: 34 Points: 6,378, Level: 34
Activity: 28%
Activity: 28% Activity: 28% Activity: 28%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,822
Thanks: 12
Thanked 14 Times in 14 Posts
Send a message via AIM to mat41
Default password validation check for letters and numbers

This function seems to work except for the checking for a string that contains letters and at least one number. please help?

function ValidatePW() {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
var fld = document.updateForm.tUserPassword

if (fld.value == "") {
fld.style.background = 'Yellow';
alert("You didn't enter a password.\n");
} else if ((fld.value.length < 8) || (fld.value.length > 15)) {
alert("The password must be a minimum of eight characters.");
fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
alert("The password contains illegal characters, enter letters and numbers only.");
fld.style.background = 'Yellow';
} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
alert("The password must contain at least one number.");
fld.style.background = 'Yellow';
} else {
fld.style.background = 'White';
}
}

TYIA
__________________
Wind is your friend
Matt
Reply With Quote
  #2 (permalink)  
Old August 27th, 2009, 01:11 AM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

Hi, Please check the following function:
Code:
<script type="text/javascript">
  function checkForm(form)
  {
    if(form.username.value == "") {
      alert("Error: Username cannot be blank!");
      form.username.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(form.username.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
      form.username.focus();
      return false;
    }
    if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
      if(form.pwd1.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        form.pwd1.focus();
        return false;
      }
      if(form.pwd1.value == form.username.value) {
        alert("Error: Password must be different from Username!");
        form.pwd1.focus();
        return false;
      }
      re = /[0-9]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one number (0-9)!");
        form.pwd1.focus();
        return false;
      }
      re = /[a-z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one lowercase letter (a-z)!");
        form.pwd1.focus();
        return false;
      }
      re = /[A-Z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one uppercase letter (A-Z)!");
        form.pwd1.focus();
        return false;
      }
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      form.pwd1.focus();
      return false;
    }
    alert("You entered a valid password: " + form.pwd1.value);
    return true;
  }
</script>
Above script checks for following:
a) at least n characters
b) combination of upper- and lower-case characters
c) one or more digits
d) not related to other user data (name, address, username, ...)
e) not a dictionary word
http://www.the-art-of-web.com/javascript/validate/1/
__________________
Om Prakash Pant
Click the "Thanks" button if this post helped you.
Reply With Quote
The Following User Says Thank You to om_prakash For This Useful Post:
mat41 (August 27th, 2009)
  #3 (permalink)  
Old August 27th, 2009, 01:22 AM
Friend of Wrox
Points: 6,378, Level: 34
Points: 6,378, Level: 34 Points: 6,378, Level: 34 Points: 6,378, Level: 34
Activity: 28%
Activity: 28% Activity: 28% Activity: 28%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,822
Thanks: 12
Thanked 14 Times in 14 Posts
Send a message via AIM to mat41
Default

Excellent that works a treat. Thankyou for your time :o)
__________________
Wind is your friend
Matt
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to number with letters & numbers JoshC XSLT 4 February 25th, 2009 11:11 AM
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 02:30 PM
Remove letters from numbers Corey Access 7 December 18th, 2005 08:09 PM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
Taking both numbers and letters as input HateMe C# 1 May 13th, 2004 11:33 PM



All times are GMT -4. The time now is 03:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.