PDA

View Full Version : validating password



pandu
Jan 8th, 2005, 05:11 PM
i am trying to validate password. my requirement is it should not contain morethan 4 letters of username and it should contain alphabets(small and big) numbers and special characters.everything is working except 4 letters of their username.if they enter more than 4 letters it's giving alert and submitting the form and if username have multiple same letters it's not working. here is my code. i appreciate if any help

function goAddChange()
{
var form1=window.document.frmLogIn

if(form1.txtUserID.value == null || form1.txtUserID.value.length == 0)
{
alert('Please Enter User Name.');
form1.txtUserID.focus();
return false;
}
if(form1.txtPWD.value == null || form1.txtPWD.value.length == 0 )
{
alert('Please Enter Password.');
form1.txtPWD.focus();
return false;
}
if(form1.txtNewPWD.value == null || form1.txtNewPWD.value.length == 0 )
{
alert('Please Enter New Password.');
form1.txtNewPWD.focus();
return false;
}
if(form1.txtNewPWD.value.length < 8 )
{
alert('New Password Should Be Eight Characters.');
form1.txtNewPWD.focus();
return false;
}
if(window.document.frmLogIn.txtRNewPWD.value !=window.document.frmLogIn.txtNewPWD.value)
{
alert('New password and Retype New Password are not same')
form1.txtRNewPWD.focus();
return false;
}

//password validation
var counter=0;
for (var i = 0 ; i < form1.txtUserID.value.length ; i++)
{
var character = form1.txtUserID.value.charAt(i);
if(form1.txtNewPWD.value.indexOf(character) > -1)
{
counter++;
}
}
if(counter > 4)
{
alert("New Password Must Not Contain All Or Part Of User Name!");
form1.textNewPWD.value='';
form1.txtNewPWD.focus();
return false;
}//End password validation

var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lower = "abcdefghijklmnopqrstuvwxyz";
var numbers = "0123456789";
var special = "[email protected]#$%^*/&?";
if (form1.txtUserID.value.indexOf(form1.txtNewPWD.value) == -1)
{
var a = 0;
var b = 0;
var c = 0;
var d = 0;
for (var i = 0 ; i < form1.txtNewPWD.value.length ; i++)
{
//alert(form1.password.value.charAt(i));
if (upper.indexOf(form1.txtNewPWD.value.charAt(i)) > -1)
{
a = 1;
}
else if (lower.indexOf(form1.txtNewPWD.value.charAt(i)) > -1)
{
b = 1;
}
else if (numbers.indexOf(form1.txtNewPWD.value.charAt(i)) > -1)
{
c = 1;
}
else if (special.indexOf(form1.txtNewPWD.value.charAt(i)) > -1)
{
d = 1;
}
}
if((a+b+c+d) < 3)
{
alert('Password must contain atleast 3 of the following 4
categories.\n English uppercase characters (A through Z). \n English
lowercase characters (a through z). \n Base 10 digits (0 through 9). \n
Non-alphanumeric characters (e.g., !, $, #, %).');
form1.txtNewPWD.focus();
return false;
}
else
{
//alert('Valid Password.');
return true;
}
}
else
{
//alert('Incorrect Password, Must not contain all or part of your user aaaccount name.');
//form1.txtPassword.focus();
return false;

}
}

Philip M
Jan 9th, 2005, 09:04 AM
This has been requested before, so I have the idea it is a homework
assignment.

Hint:- search this forum for posts by ugly

pandu
Jan 9th, 2005, 05:38 PM
if you know can you please post code. ugly's requirement is different compare to mine. i tried with his code. it didn't work.

ugly
Jan 10th, 2005, 08:59 PM
can anyone give me idea or solution to fix my problem.

thanks.