[thelist] Simple Javascript field checking

john at johnallsopp.co.uk john at johnallsopp.co.uk
Wed Mar 2 04:25:11 CST 2005


>> Server-side is the only way to be certain.
> Yes it is, but you can have both. What you should do to make it
> failsafe
> (of course with a backend script to fall back on) is to call your
> script
> onsubmit on the form not onclick of a button. Your example would make
> the button useless when there is no scripting available.

Thanks for all that. I'm trying to do it belt and braces. I like an
alert straight away if I've just made an error in a field, and just in
case, I may as well run the same checks before form submission.
However, security and Javascript-off issues mean I need to write and
run the same script functions but server side too. I'm checking three
times. Perhaps the only superfluous one is the final JavaScript form
check, but I'm happier with it there, just in case I've made a
programming error (such as the one you describe where password field 1
is the one that gets amended) .. it takes no real additional time
compared to a web page reload.

OK, so the problem I now have is with focus(). The following works
with IE6/XP, but not with Firefox1.0/Linux Red Hat 9. Shall I just
live with that, or have I missed something?

I removed the second email field, so let's deal with the same issue in
the Password checker:

function checkPassword()
{
	if
(document.registrationForm.Password.value!=document.registrationForm.Password2.value
|| document.registrationForm.Password.value=="" ||
document.registrationForm.Password==null)
	{
		alert ("Passwords missing or not the same");
		document.getElementById("Password2").focus();
		return false;
	}
	return true;
}

and:

		<tr>
			<td><label for="password">Password: </label></td>
			<td><input type="password" name="Password" id="Password"></td>
		</tr>
		<tr>
			<td><label for="password2">Password (repeat): </label></td>
			<td><input type="password" name="Password2" id="Password2"
onBlur="checkPassword();"></td>
		</tr>


Cheers
J


More information about the thelist mailing list