/    Sign up×
Community /Pin to ProfileBookmark

form validation script not working

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>

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 27.2003 — [font=arial][color=maroon]Make sure your form tag looks something like this:[/color][/font]

[font=monospace]

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

[/font]

[font=arial][color=maroon]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.[/color][/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@CharlesSep 27.2003 — [i]Originally posted by lhauser63 [/i]

[B]pw1 = form.pw1.value;

pw2 = form.pw2.value; [/B]
[/QUOTE]
[font=georgia]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:[/font]

[font=monospace]if(form.pw1.value != form.pw2.value) {("nYou did not enter the same new password twice. Please re-enter your password."); return false}[/font]
Copy linkTweet thisAlerts:
@lhauser63authorSep 27.2003 — 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???
Copy linkTweet thisAlerts:
@Khalid_AliSep 27.2003 — show the html code that calls/uses this javascript
Copy linkTweet thisAlerts:
@lhauser63authorSep 27.2003 — 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
Copy linkTweet thisAlerts:
@JonaSep 27.2003 — [i]Originally posted by lhauser63 [/i]

[B]<cfform target="form" action="http://www.mobilexperts.com/sim.php" method="post" onSubmit="checkPW(this)">[/B][/QUOTE]


[font=arial][color=maroon]That should be...[/color][/font]

[font=monospace]

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

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@Khalid_AliSep 27.2003 — [i]Originally posted by Jona [/i]

[B][font=arial][color=maroon]That should be...[/color][/font]



onsubmit="checkPw(this);">



[b][J]ona[/b] [/B]
[/QUOTE]


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)">
Copy linkTweet thisAlerts:
@JonaSep 27.2003 — [i]Originally posted by Khalid Ali [/i]

[B]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)"> [/B]
[/QUOTE]


[font=arial][color=maroon]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.[/color]
[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@lhauser63authorSep 27.2003 — Thanks everyone for the help. I really Appreciate it!!

Laura
Copy linkTweet thisAlerts:
@JonaSep 28.2003 — [font=arial][color=green]You're welcome, for my part. ?[/color][/font]

[b][J]ona[/b]
×

Success!

Help @lhauser63 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...