Click to See Complete Forum and Search --> : Validation trouble


CHAKENHOOD
08-26-2003, 07:02 PM
I have tried to create required fields on my website. The validation starts onSubmit an will give an alert if a field is left empty but will submit the form anyway. please help.

<script language="JavaScript">
function validate() {
if (document.contactform.name.value.length < 1) {
alert("Please enter your full name.");
return false;
}
if (document.contactform.email.value.length < 7) {
alert("Please enter a valid E-Mail address.");
return false;
}
if (document.contactform.comments.value.length < 1) {
alert("Please enter your questions or comments.");
return false;
}
return false;
}
</script>

<form name="contactform" method="post" action="contactaction.php" onSubmit="validate();">

Shampie
08-26-2003, 08:23 PM
if (document.contactform.name.value.length < 1) {

replace with:

if (document.contactform.name.selectedIndex==0){
continue = false;
}

if (!continue){
alert("Please make sure the field was properly completed.");
return false;
}
else
return true;
}

something like that?

pyro
08-27-2003, 07:08 AM
Take a look at either http://www.infinitypages.com/research/formvalidation.htm to validate that all the fields in a form have a value entered or http://www.infinitypages.com/research/formvalidation_2.htm to check only the specified fields. It also uses regexp, to test that the fields don't only contain blank spaces...