Hi,

I really could use some help. I realize there are probably lots of posts on validating forms with Javascript, but I have been at this all day and been pouring through Google and I haven’t found what I’m looking for – I feel like my validation is close, but I just can’t get it to work in two areas. They may be related; they may not, but I’m really stuck. I have a contact form (only three fields, name, email, and message). I want to make sure that there is something in each field and that the email is at least sort of legit before the message is sent through.

I’ve been trying to mesh tutorials and this is what I’ve come up with. However, my “Your message has been sent” alert which I was trying to apply at the end (if everything was verified) never comes through and I’m absolutely stumped. Any pointers would be greatly appreciated.

Code:
<script type="text/javascript">
function checkform()
{
	if (document.contactAPC.name.value == '')
	{
		alert('Please enter a name.');
		return false;
	}
	else if(document.contactAPC.email.value == '')
	{
		alert('Please enter an email address.');
		return false;
	}
	else if(document.contactAPC.message.value == '')
	{
		alert('Please write a message.');
		return false;
	}
	else if(document.contactAPC.email.value != '')
	{	
	emailpattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
		if(!emailpattern.test(document.contactAPC.email.value)) 
			{
			 alert("Please enter a valid email address");
			 return false;
			}
	}
	else 
	{
		//Why can't I get this message to show?
				return true;
				alert("Your message has been sent.");
	}
}
</script>


</head>
<body>
<h2>Contact Us</h2>
	<form name="contactAPC" method="post" action="" onSubmit="return checkform()"> 
	Name <input type="text" size="26" id="name" name="name" class="txtfield_details"> 
	<br /> 
	<br />Email <input type="text" size="26" id="email" name="email" class="txtfield_details"> 
	<br /> 
	<br /> 
	Question/Comment
	<br /><textarea cols="24" rows="4" class="txtfield_details" name="message" id="message"></textarea> 
	<br /> 
	<br /><input type="submit" value="Submit" name="submit" class="btns" /> 
	<input type="reset" value="Clear" name="clear" class="btns" /> 
	</form>
Now my other issue is that when I attach the php file (below) to the form action (ie. action=”myfile.php”) – the form no longer even bothers to remotely try to validate. It just sends the contents straight through and I get my “Your message has been sent message” and this also confuses me. I thought checkform() needed to come back as true for the submit button to move the user forward.

Code:
<?php
$email = $_REQUEST['email'] ;

$message = 
$_POST["name"] ."\n". 
$_POST["email"] ."\n<>".
$_POST["message"] ."<>";

mail( "myemail@yahoo.com "From my Website",
$message, "From: $email" ); 
header("Location: http://www.mywebsite.org");
?>
If anyone could shed some light on this, it would make my day. It’s just been over a year since I’ve done Javascript and I never quite got the form validation to work right and it just a bit frustrating.

Thanks.