PDA

View Full Version : Field Comparison in Javascript



tsbarnes
Mar 19th, 2003, 03:29 PM
Ok,

I have two fields in a form:

Password:
ConfirmPassword:

I am trying to write a javascript that will compare the values of these two fields and if they do not match then return an error if they do then go ahead and submit the form.

Thanks in advance,

Tsbarnes

ACJavascript
Mar 19th, 2003, 04:22 PM
here ya can do this

<script language="javascript">

function check(){

if(document.theform.psw.value==document.theform.psw2.value){

//Do whatever if it matchs

}else{
alert("Sorry but your passwords do not match")
}

}
</script>

that should work :D

Happyscripting :D

cheesebagpipe
Mar 20th, 2003, 12:18 AM
<html>
<head>
<script type="text/javascript">

function chkPWD(first, second) {
var el, msg = '';
if (first.value == '' || /^\s+$/.test(first.value)) {
msg = 'Please enter a password.';
el = first;
}
else if (second.value == '' || /^\s+$/.test(second.value)) {
msg = 'Please re-enter your password.';
el = second;
}
else if (second.value != first.value) {
msg = 'Please ensure that your password & confirmed password are the same.';
el = second;
}
if (msg) {
alert(msg);
el.focus();
el.select();
return false;
}
return true;
}

</script>
</head>
<body>
<form onsubmit="return chkPWD(pwd1,pwd2)">
Password:<input type="password" name="pwd1"><br />
Confirm Password:<input type="password" name="pwd2"><br /><br />
<input type="submit">
</form>
</body>