Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 27th, 2009, 12:27 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 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
 
Old August 27th, 2009, 01:11 AM
Friend of Wrox
 
Join Date: May 2004
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.
The Following User Says Thank You to om_prakash For This Useful Post:
mat41 (August 27th, 2009)
 
Old August 27th, 2009, 01:22 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to number with letters & numbers JoshC XSLT 4 February 25th, 2009 12:11 PM
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 03:30 PM
Remove letters from numbers Corey Access 7 December 18th, 2005 09: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.