Here is a function I wrote to validate passwords in Javascript.
Rules:
1. capital and lower-case letters
2. at least one digit
3. not the same as their user name
4. not the same as the old password
5. not blank
6. minimum length
7. at least one letter
8. no white-space
9. does not contain certain names
There are a couple of things here that aren't best-practice,
but I was required to tack this on to an existing
web application, without the freedom to do much to
the original form. I hope you find some of this
to be helpful.
To answer your original question, here is a quickie
that may be helpful.
function checkForDigit(strTest){
if (strTest.match(/\d/)){
alert('\'' + strTest + '\' contains a digit');
if (strTest.match(/^\d+$/)){
alert('\'' + strTest + '\' is completely numeric.');
}
}else{
alert('\'' + strTest + '\' does not contain a digit');
}
}
checkForDigit('nstaoheu');
checkForDigit('nst4oheu');
checkForDigit('a');
checkForDigit('4');
Shawn
//password validation.js
function validatePass(formName, oldPass, newPass1, newPass2){
//function written on September 18th, 2003
//by Shawn Milochik (Milo LinuxMail Org)
//
//Revisions: none
//
//declare variables we will use
var returnValue = true; //will decide whether or not to submit the
form
var strOldPass = eval('document.' + formName + '.' + oldPass +
'.value');
var strNewPass1 = eval('document.' + formName + '.' + newPass1 +
'.value');
var strNewPass2 = eval('document.' + formName + '.' + newPass2 +
'.value');
var errMessage = '';
//For testing, to show what was entered for the old
//and new passwords
//alert(strNewPass1 + ', ' + strNewPass2);
var minLength = 7; // change this to change the minimum password
length
var strUID = document.location + ''; //get the querystring from the
browser so we can get the Uid
//Time-limited test stuff
//replace the actual querystring with the sample
//pulled from the real page
if (new Date('9/26/2003 15:00:00') > new Date()){
alert('testing mode, using fake querystring');
strUID = '
http://gebis/mstr7/ChangePassword.a...1500083903038D204&Port=0&Uid=dwuser&UMode=1';
}
//get the Uid from the querystring, and put it in strUID
//of the Uid in the querystring.
strUID = strUID.replace(/^.*\&Uid=(\w+)\&.*$/, "$1");
// Regular Expression translation for above:
// ^ = beginning of the line
// . = any character
// * = any number of the preceeding value, including zero
// \& = & (& is a special character, and needs to be
escaped)
// Uid = find the string "Uid"
// \w = a word character (a-z, A-Z, 0-9, _)
// + = one or more consecutive positions
// \& = & (& is a special character, and needs to be
escaped)
// . = any character
// * = any number of the preceeding value, including zero
// $ = the end of the line
//
// Using parenthesis around part of the expression allows us
to use
// the matching text in the replace function. In this case,
(\w+)
// allowed us to return whatever \w+ matched as $1. If we
had used
// another set of parenthesis, we could refer to the second
one as
// $2, and so on.
//
//See "Mastering Regular Expressions" by Jeffrey Friedl for all
this and more.
//
//**********************************************************
// Rule: Must contain both upper and lower case
//**********************************************************
//Check for all lower-case
if (strNewPass1.toLowerCase() == strNewPass1){
//alert('New password must contain at least one upper-case
character.');
errMessage = errMessage + '\n' + 'New password must contain at
least one upper-case character.';
returnValue = false;
}
//Check for all upper-case
if (strNewPass1.toUpperCase() == strNewPass1){
//alert('New password must contain at least one lower-case
character.');
errMessage = errMessage + '\n' + 'New password must contain at
least one lower-case character.';
returnValue = false;
}
//**********************************************************
// Rule: password does not match the user name
//**********************************************************
if (strNewPass1 == strUID){
alert('New password may not be the same as your user ID.');
returnValue = false;
}
//**********************************************************
// Rule: new password is the same in both password boxes
//**********************************************************
if (strNewPass1 != strNewPass2){
alert('New passwords do not match.');
returnValue = false;
}
//**********************************************************
// Rule: password is not the same as the old
//**********************************************************
if (strNewPass1 == strOldPass){
alert('New passwords may not be the same as the old one.');
returnValue = false;
}
//**********************************************************
// Rule: password is not blank
//**********************************************************
if (strNewPass1 == ''){
alert('New password may not be left blank.');
returnValue = false;
}
//**********************************************************
// Rule: password meets minimum length requirements
//**********************************************************
if (strNewPass1.length < minLength){
alert('New password may not less than ' + minLength + '
characters long.');
returnValue = false;
}
//**********************************************************
// Rule: password does not contain the name of a Campbell brand
//**********************************************************
if (strNewPass1.match(/((arnotts|campbell|franco|godiva|homepride|pace|prego|stockpot|swanson|v-?8|pepperidge|goldfish))/)){
alert('New password may not contain the name of a Campbell
brand.');
returnValue = false;
}
//**********************************************************
// Rule: password contains at least one letter
//**********************************************************
if (strNewPass1.match(/^[0-9]+$/)){
alert('New password must contain both letters and numbers.');
returnValue = false;
}
//**********************************************************
// Rule: password contains at least one digit
//**********************************************************
if (strNewPass1.match(/^[a-zA-Z]+$/)){
//alert('New password must contain both letters and numbers.');
errMessage = errMessage + '\n' + 'New password must contain both
letters and numbers.';
returnValue = false;
}
//**********************************************************
// Rule: password contains no spaces, tabs, etc
//**********************************************************
if (strNewPass1.match(/\s/)){
//alert('New password may not contain spaces.');
errMessage = errMessage + '\n' + 'New password may not contain
spaces.';
returnValue = false;
}
if (returnValue == false){ alert(errMessage); }
//if this returns false to the onSubmit() function within
//the <form> tag, then the form submission will be cancelled
return returnValue;
}