form validation problem

They have: 71 posts

Joined: Oct 2005

can anyone help me with why this form in NOT validating! My code is Below!

Untitled Document

<script language=javascript>
function verifyRequired() {
if (document.icpsignup.fields_email.value == "") {
alert("The email field is required.");
return false;
}

if (
!document.icpsignup["listid:16405"].checked &&
!document.icpsignup["listid:16052"].checked &&
true) {
alert("The Lists field is required.");
return false;
}

return true;
}
</script>

First Name

Last Name

Email Address

Newsletter

Tips

Danny G Guillory Jr
[email protected]
337.303.6453

Busy's picture

He has: 6,151 posts

Joined: May 2001

If that page is called signup.pl the javascript validation should be before the form, though javascript is useless for validation as it can be disabled, really, if you can use server side (PHP, ASP, CF etc)
Also the redirect shouldn't be on the page, anyone can just look at the source and go directly to the redirect without submitting the form

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Also, you need to put the following after your <script> tag:

// <[CDATA[

and the following before your </script> tag:
// ]]>

As you're using XHTML, don't use the old comment method in your JavaScript! It will work for now, but as soon as you serve that document with a MIME type of application/xhtml+xml the parser will throw all the code in comments out! For example, using the old method of hiding JavaScript from Validators and old browsers:
&lt;script type="text/javascript"&gt;
<!--
    .... code ....
-->
&lt;/script&gt;

And the new method (which is what should be used with XHTML):
&lt;script type="text/javascript"&gt;
// <[CDATA[
    .... code ....
// ]]>
&lt;/script&gt;

You should use the second method with XHTML, as when the MIME Type is changed to application/xhtml+xml comments are entirely ignored by the browser, even if they're scripts (just the way that XML works).

The second method uses comments, but only single line ones so that browsers don't choke on a <[CDATA[ declaration being within a <script> tag, which is illegal as far as the browser is concerned. Also those are JavaScript comments, so an XML parser won't ignore their contents.

If you really want to avoid all these headaches, then put all your JavaScript into external files. if you want to get really semantic avoid the onclick event and use addEventListener in external files instead. This really seperates code from content, and when you're done you can look over your code with great satisfaction. Smiling

--------------------------------

The only other problem is that you're not putting quotes around your attribute values. For example, instead of this:

<input type=hidden name="specialid:16052" value="7EXT">

You should have:
<input type="hidden" name="specialid:16052" value="7EXT">

Notice the quotation marks around hidden. All XHTML attributes need to contained within quotation marks like this, it's one of the rules of the language.

There may be more problems, but those two are the big ones. Good luck with fixing them and come back if you get stuck. Smiling

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.