View Full Version : Form Validation using getElementbyId

03-13-2006, 01:14 PM
i have modifed a script by Stephen Poley and have got it to work correctly:

However, i need the page to be in Strict XHTML 1.1 so i can't do <form name="contactform">. When i try and use the getElementbyId('name') instead of document.forms.contactform.name the script doesnt work.

what am i doing wrong?

<script type="text/javascript">
// Only script specific to this form goes here.
// General-purpose routines are in a separate file.
function validateOnSubmit() {
var elem;
var errs=0;
// execute all element validations in reverse order, so focus gets
// set to the first one in error.
if (!ValidateOther(document.getElementbyId('name'), 'nameinfo')) errs += 1;
if (!validateEmail(document.getElementbyId('email'), 'emailinfo', true)) errs += 1;
if (!ValidateOther(document.getElementbyId('subject'), 'subjectinfo')) errs += 1;
if (!ValidateOther(document.getElementbyId('message'), 'messageinfo')) errs += 1;
if (errs>1) alert('There are fields which need correction before sending');
if (errs==1) alert('There is a field which needs correction before sending');

return (errs==0);
// -->

<form onsubmit="return validateOnSubmit()" action="http://www.cems.uwe.ac.uk/~tdrewry/cgi/cgiform.cgi" method="post">

<label for="name">*Name:</label><br />
<input type="text" name="name" id="name" onchange="ValidateOther(this, 'nameinfo');" />
<label id="nameinfo">&nbsp;</label><br />

<label for="email">*Email:</label><br />
<input type="text" name="email" id="email" onchange="validateEmail(this, 'emailinfo', true);" />
<label id="emailinfo">&nbsp;</label><br />

<label for="subject">*Subject:</label><br />
<input type="text" name="subject" id="subject" onchange="ValidateOther(this, 'subjectinfo');" />
<label id="subjectinfo">&nbsp;</label><br />

<label for="message">*Message:</label><br />
<textarea name="message" id="message" onchange="ValidateOther(this, 'messageinfo');" cols="255" rows="9"></textarea>
<label id="messageinfo">&nbsp;</label><br />

<input type="submit" name="Submit" value="Send" />


the whole page can be found here (http://www.thelogoarchive.com/dylan/contact.html)

03-13-2006, 11:25 PM
In what way does it not work?

03-14-2006, 12:21 AM
if you click on the link
http://www.thelogoarchive.com/dylan/contact.html and press send with invalid data it is suposed to pop up with a box saying "There are fields which need correction before sending".

if you save the file to your computer and change the getelimentbyid to forms.contact.*insert value such as name* and put a name=contact thing in the form tag it will work.

but strict xhtml 1.0 doesnt suport name inside a form tag.

03-14-2006, 12:41 AM
JavaScript is case-sensitive.

There is no

you mean


03-14-2006, 01:13 AM
ahh brilliant,