Click to See Complete Forum and Search --> : form validation script not working


lhauser63
09-26-2003, 10:09 PM
I'm new to JavaScript and downloaded a script to verify two password fields in a form. I added it to my code, but it doesn't do anything at all. I put the script in the head tag and added the onSubmit to point to my script -- just like the instructions.

What am I doing wrong?

Here's the script:

<!-- Begin
function checkPw(form) {
alert("got here")
pw1 = form.pw1.value;
pw2 = form.pw2.value;

if (pw1 != pw2) {
alert ("\nYou did not enter the same new password twice. Please re-enter your password.")
return false;
}
else return true;
}
// End -->
</script>

Jona
09-26-2003, 11:35 PM
Make sure your form tag looks something like this:


<form action="" onsubmit="checkPw(this);">


Also ensure that you have <input type="password" name="pw1"> and <input type="password" name="pw2"> in that same form--otherwise you will get an error.

[J]ona

Charles
09-27-2003, 06:53 AM
Originally posted by lhauser63
pw1 = form.pw1.value;
pw2 = form.pw2.value; There are several problems with that, among them that they should each be preceeded by "var". But they are unnecessary and clutter the namespace and take up garbage collection time. Just delete them and change your "if" statement to:

if(form.pw1.value != form.pw2.value) {("\nYou did not enter the same new password twice. Please re-enter your password."); return false}

lhauser63
09-27-2003, 10:46 AM
Thanks guys, I tried all of those suggestions, but the javascript is never called. The first alert is not even displayed. I get an error on the page that says it cannot find an object?

This looks straight forward -- what can it be???

Khalid Ali
09-27-2003, 11:22 AM
show the html code that calls/uses this javascript

lhauser63
09-27-2003, 12:28 PM
Hey guys. Thanks for keeping on this with me. Here's the rest of the code:

<cfform target="form" action="http://www.mobilexperts.com/sim.php" method="post" onSubmit="checkPW(this)">
<tr>
<td></td>
<td>User Name</td>
<td><input name="UserName" type="text" size="50" maxlength="50"></td>
</tr>
<tr>
<td></td>
<td>Password</td>
<td><input name="pw1" type="password" size="50" maxlength="50"></td>
</tr>
<tr>
<td></td>
<td>Confirm Password</td>
<td><input name="pw2" type="password" size="50" maxlength="50"></td>
</tr>
<tr>

<tr>
<td></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td></td>
<td>&nbsp;</td>
<td><input name="submit" type="submit" value="Continue" ></td>
</tr>
</table>

</cfform></td>


Laura

Jona
09-27-2003, 12:29 PM
Originally posted by lhauser63
<cfform target="form" action="http://www.mobilexperts.com/sim.php" method="post" onSubmit="checkPW(this)">

That should be...


<form target="form" name="form" action="http://mobilexperts.com/sim.php" method="POST" onsubmit="checkPw(this);">

[J]ona

Khalid Ali
09-27-2003, 01:11 PM
Originally posted by Jona
That should be...

onsubmit="checkPw(this);">

[J]ona

There is another error in onsubmit event

you are calling checkPw where as the actual function you have in JS section is
checkPW
notice the CAPS for PW, and you need to add a return before the function call. The correct syntax will be

onsubmit="return checkPW(this)">

Jona
09-27-2003, 01:13 PM
Originally posted by Khalid Ali
There is another error in onsubmit event

you are calling checkPw where as the actual function you have in JS section is
checkPW
notice the CAPS for PW, and you need to add a return before the function call. The correct syntax will be

onsubmit="return checkPW(this)">

Khalid, according to the original code posted, it is checkPw() not checkPW(). His form has checkPW() in it, but it is not correct.

You are right about the return checkPw(this); part, though.

[J]ona

lhauser63
09-27-2003, 05:06 PM
Thanks everyone for the help. I really Appreciate it!!

Laura

Jona
09-27-2003, 07:17 PM
You're welcome, for my part. :)

[J]ona