[RESOLVED] Form validating issues; form action to php file negates validation, why?
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.
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.
I knew it was something simple (and quite possibly stupid). A mentor helped me catch the error(s), which resolved both issues.
For anyone else who might ever have a similar problem. In the last, else-statement, I need to have return true; come AFTER the alert box so that the script does exit before I need the last action to run.
Also I had (although not on this copy) a comment to hide the javascript from older browsers at the bottom of the script:
Code:
//stop hiding script -->
I didn't have the comment on it and it was spitting out a error (apparently in Firefox), although IE8 was reading everything fine/no errors, so that's why I was just really confused.
I love helpful people. Fixing this did really make my evening so much better. : )
[QUOTE]In the last, else-statement, I need to have return true; come AFTER the alert box so that the script does exit before I need the last action to run.
/QUOTE]
Bookmarks