Click to See Complete Forum and Search --> : still trying to validate (i suck)


rdharris
02-07-2003, 08:13 AM
Well I've tried more things.
The code has ended up looking like this, and
is still not working...

.. this is a continuation from:
http://www.jsworkshop.com/bb/viewtopic.php?t=617

****************************
http://www1.gnb.ca/0007/Culture/Heritage/VMC/mailer/sendmail-e.asp
****************************


function RequiredFields()
{
if( Validate() )
{
// validation passed, allow post
document.frmInfo.submit();
} // validation failed, disallow post return false;
}


function Validate()
{
//--- Make sure the Name was entered
if(document.frmInfo.txtName.value.length == 0){
document.frmInfo.txtName.focus();
document.frmInfo.txtName.select();
alert("You must enter your name to continue.");
return false;
}
//--- Make sure the EMail Address was entered
if(document.frmInfo.txtEMail.value.length == 0){
document.frmInfo.txtEMail.focus();
document.frmInfo.txtEMail.select();
alert("You must enter an EMail address to continue.");
return false;
}
//--- Make sure the Comments are entered
if(document.frmInfo.txtComments.value.length == 0){
document.frmInfo.txtComments.focus();
document.frmInfo.txtComments.select();
alert("You must enter your comments to continue.");
return false;
}
return true;
}

***********************************

<form name="frmInfo" method="POST" action="mail.asp">
<INPUT TYPE="hidden" NAME="txtAction" VALUE="">

<INPUT TYPE="button" VALUE="Submit" name="btnSubmit" onclick="RequiredFields()">
***********************************

Charles
02-07-2003, 08:33 AM
I took a closer look at your page and two things struck me immediately. To begin with, your HTML is very, very bad. You've got two different pages run together there. Put the following at the top of you page and make sure that it passes the validator at http://validator.w3.org/:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

It is possible, nay likely, that the page is just too confusing for the JavaScript to work.

The second thing I notice is that you are needlessly making a form that will fail completely when ever JavaScript is absent. You need to be validating from the onsubmit handler of the FORM element and you need a real SUBMIT button. JavaScript free users won't get validated but at least they'll be able to submit the form.

<form name="frmInfo" method="POST" action="mail.asp" onsubmit="RequiredFields()">
<div>
<INPUT TYPE="hidden" NAME="txtAction" VALUE="">

<INPUT TYPE="submit" VALUE="Submit" name="btnSubmit">
</div>
</form>

khalidali63
02-07-2003, 08:39 AM
Try this..

cheers

Khalid