...

View Full Version : Request for Javascript code for comparing two passwords in two different textboxes



itsvineeth209
06-11-2008, 05:53 PM
I request you send me Javascript code for comparing two passwords in two different textboxes

mrhoo
06-11-2008, 07:55 PM
This will work if you have exactly two password fields:


function comparepasswords(){
var A= document.getElementsByTagName('input');
var L= A.length, i= 0,tem,P= [];
while(i<L){
tem= A[i++];
if(tem.type.toLowerCase()== 'password')P[P.length]=tem;
}
if(P[0].value && P[0].value=== P[1].value)return true;
else return false;
}

Philip M
06-11-2008, 08:05 PM
Javascript is totally unsuitable for password/logon as the source code can be read by anyone.

Having said that:-



<form name = "myform">
<input type = "text" name = "pwd1" id = "pwd1" size = "12"><br>
<input type = "text" name = "pwd2" id = "pwd2" size = "12"><br>
<input type = "button" value = "Click here to verify password" onclick = "pwdCompare()">
</form>

<script type = "text/javascript">

function pwdCompare() {
var p1 = document.myform.pwd1.value;
p1 = p1.replace(/^\s+/g, ""); // strip leading spaces
var p2 = document.myform.pwd2.value;
p2 = p2.replace(/^\s+/g, ""); // strip leading spaces
if ((p1.length < 6) || (p1 != p2)) { // minimum 6 characters
alert ("The two passwords do not match or have too few characters! Try again!");
document.myform.pwd1.value = "";
document.myform.pwd2.value = "";
document.myform.pwd1.focus();
return false;
}
else {
alert ("The passwords match!");
}
}

</script>



All this does is checks that the two passwords match (case sensitive) and have a minimum of 6 characters. You will need further verification if the passwords have to have a minimum number of alpha characters, or some combination of letters/numbers/special characters.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum