...

View Full Version : alphanumeric password validation problem



angst
12-20-2006, 03:42 PM
hello,
I'm trying to do alphanumeric password validation,

I've found a script on another forum, but I've having issues with one part of it.

this:


var sizechar = 5;//length for password

var alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

for (var i=0; i<sizechar; i++) {
if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
alphaCount++
}
}

part seems to be a problem area.

If i use a password like "123456" it seems to be counting all of these as characters. and I don't know even about javascript to solve the problem.

any help would be great,

here's the entire script:



<script>
function ValidateForm(){

var sizechar = 5;//length for password
var upassID=document.PasswordTest.password;

//check if passsword is filled out
if ((upassID.value==null)||(upassID.value=="")){
alert("Please enter your password");
upassID.focus();
return false;
}
//add regex alphanumeric check here
if (upassID.length > sizechar) {
alert('Your password is too long');
upassID.focus();
return false;
}
if (upassID.length < sizechar) {
alert('Your password is too short');
upassID.focus();
return false;
}
var upass_string = upassID.value;

var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (var i=0; i<sizechar; i++) {
if (valid.indexOf(upass_string.charAt(i)) < 0) {
alert('Your password contains invalid characters');
upassID.focus();
return false;
}
}

var alphaCount = 0;
var numCount = 0;
var num_valid="0123456789";

for (var i=0; i<sizechar; i++) {
if(num_valid.indexOf(upass_string.charAt(i)) < 0) {
numCount++
}
}
if(numCount==upass_string.length){
alert('Your password contains only characters. Please enter an alphanumeric value like -alpha1-');
upassID.focus();
return false;
}

var alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

for (var i=0; i<sizechar; i++) {
if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
alphaCount++
}
}
alert("Alpha Counter: " + alphaCount);
if(alphaCount==upass_string.length){
alert('Your password contains only numbers. Please enter an alphanumeric value like -alpha1-');
upassID.focus();
return false;
}
return true;
}
</script>

<form name='PasswordTest' id='PasswordTest' >
<input type='text' name='password' id='password' />
<input type='button' value='check password' onclick='return ValidateForm();' />
</form>



thanks in advance for your time!
-Ken

Philip M
12-20-2006, 05:18 PM
My undersatnding is that he wants a password of exactly five letters, ignoring case, and no numbers or other characters.

function validate(nField){

var userInput = nField.value;
if (!/^[A-z]{5}$/.test(userInput))
{
alert('Only five letters are allowed');
nField.value = "";
nField.focus();
}
}

Obviously {5} can be changed to some other avlue if required, or say {5,8} to permit min of 5 and max of 8 character.

angst
12-20-2006, 05:28 PM
hello,
thanks for your replies!

but i actually do want "alphanumeric" passwords,
so I would like it to continue with atleast one letter and one number, with a minimum of 5 characters length.

also like i mentioned above,
this seems to be the problem area:



for (var i=0; i<sizechar; i++) {
if (alph_valid.indexOf(upass_string.charAt(i)) < 0) {
alphaCount++
}
}


it seems to be counting numbers as letters, and i don't understand why?

thanks again for all your help,
-Ken

Philip M
12-20-2006, 05:52 PM
"but i actually do want "alphanumeric" passwords,
so I would like it to continue with at least one letter and one number, with a minimum of 5 characters length."

That is not what you asked for at first.

Try this:-

function validate(nField) {

var userInput = nField.value;

if ((userInput.length>=5) && (/!\W/.test.userInput)) { /// minimum of 5 alphanumeric characters only
if (userInput.match (/\d/g)!=null && userInput.match(/\d/g).length>=1 && userInput.match (/[a-z]/gi)!=null && userinput.match (/[a-z]/gi).length>=1 ) { /// at least one letter (ignoring case) and one number
alert ("Password is valid")
}
}
else {
alert ("Password is not valid")
}
}


Your other script should be

for (var i=0; i<sizechar; i++) {
if (alph_valid.indexOf(upass_string.charAt(i)) !=-1) {
alphaCount++ // i.e. character is found
}
}

david_kw
12-20-2006, 06:40 PM
I was testing it out for my own purpose and found a typo so figured I'd repost Philip's code in my test case in case someone was having trouble with it.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Password Validation</title>
<script type="text/javascript">
function doIt(s) {
var adiv = document.getElementById("adiv");
adiv.innerHTML = s;
}

function validate(nField) {
var userInput = nField.value;

if ((userInput.length>=5) && (!/\W/.test(userInput)) /// minimum of 5 alphanumeric characters only
&& userInput.match(/\d/g) != null
&& userInput.match(/\d/g).length >= 1 /// at least one number
&& userInput.match(/[a-z]/gi) != null
&& userInput.match(/[a-z]/gi).length >= 1 ) { /// at least one letter (ignoring case)
doIt("Password is valid");
} else {
doIt("Password is not valid");
}
}
</script>
</head>
<body>
<div>
<input type="text" name="pw" id="pw" />
<button onclick="validate(document.getElementById('pw'));">Validate It</button>
<br />
<div id="adiv"> </div>
</div>
</body>
</html>


david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum