...

View Full Version : password validation?



angst
04-25-2004, 05:47 PM
hello,
how can i make sure that passwords in two input feilds are the same with our submitting to the server?

I'm very new so an example would be great!

thanks in advance for your time!

squirellplaying
04-25-2004, 06:20 PM
You can check using javascript, but this can be disabled. You might want to use a client side language such as PHP.


<head>
<script>
function checkForSamePassword(pw1,pw2){
if (pw1 == pw2){
alert('They are the same')//Do what you want if they are the same.
}
else{
alert('They are not the same')//Do what you want if they are NOT the same.
}
}
</script>
</head>
<body>
<input type = "password" id="pass1" /><br />
<input type = "password" id="pass2" /><br />
<input type = "button" onclick = "checkForSamePassword(document.getElementById('pass1').value,document.getElementById('pass2').value)" />

Philip M
04-25-2004, 06:22 PM
Here is what I use. It also checks for a "valid" password (only alphanumerics).
Hope this is what you require. You must alter "regform" to the name of your own form.


<SCRIPT language= "JavaScript">
function testpw() {
var a = document.regform.Password.value;
var b = document.regform.Password2.value;
var c = a.replace (/[^a-zA-Z0-9]/g,"XX");
if ((a.length <6) || (c != b)) {
alert ("Your two passwords are not the same, or \\nthey contain invalid characters, or \\nthey are less than 6 characters in length. \\n\\nPlease re-enter them! \\n\\nYour password is case sensitive \\n - ABC is not the same as abc.");
document.regform.Password.value="";
document.regform.Password2.value="";
document.regform.Password.focus();
return false;
}
}
</SCRIPT>

angst
04-25-2004, 10:51 PM
ok, just one more question,
when using this script:

<head>
<script>
function checkForSamePassword(pw1,pw2){
if (pw1 == pw2){
alert('They are the same')//Do what you want if they are the same.
}
else{
alert('They are not the same')//Do what you want if they are NOT the same.
}
}
</script>
</head>
<body>
<input type = "password" id="pass1" /><br />
<input type = "password" id="pass2" /><br />
<input type = "button" onclick = "checkForSamePassword(document.getElementById('pass1').value,document.getElementById('pass2').value)" />


it continues to submit the forum even if the password do not match, like i get the alert, but i would like it to stop wheni get the alert, and give me the chance to correct the problem.

can this be done?

thanks again for your time!

glenngv
04-26-2004, 03:04 AM
<html>
<head>
<script>
function checkForSamePassword(f){
if (f.pass1.value != f.pass2.value){
alert('They are not the same');
return false;
}
return true;
}
</script>
</head>
<body>
<form name="myform" action="processpage.xxx" method="post" onsubmit="return checkForSamePassword(this)">
<input type = "password" id="pass1" name="pass1" /><br />
<input type = "password" id="pass2" name="pass2" /><br />
<input type = "submit" name="btnSubmit" value="Submit" />
</form>
</body>
</html>

angst
04-26-2004, 04:11 AM
thanks!

Philip M
04-26-2004, 08:01 AM
With respect glenngv,

function checkForSamePassword(f){
if (f.pass1.value != f.pass2.value){
alert('They are not the same');
return false;
}
return true;
}

allows the user to enter anything, even a space as a password. Or in fact enter nothing at all - the function will still return true.

IMHO it is better to positively check for alphnumerics only, and a
defined minimum password length (say 6 characters).

Also, if the passwords do not match the two boxes should be cleared and the user invited to enter them again (focus). As it is he is simply told that ******* does not match *******. Where does he go from there?

glenngv
04-26-2004, 08:22 AM
I agree with you but the original poster just ask for a script that checks 'password and confirm password' match. He/She did not mention about verifying against password policy such as minimum password length, password restrictions, etc. I just presented a basic solution. It's up to him/her to enhance it according to additional (if any) requirements.

Philip M
04-26-2004, 01:12 PM
glenngv - I can't think why, but I somehow get the impression that you work in the public sector.

glenngv
04-27-2004, 02:40 AM
What do you mean by that? :eek:
Is it a compliment or not? :rolleyes:

BTW, I work in a privately owned multinational IT company as a Web developer/analyst. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum