...

View Full Version : email address validation - won't pop up box



e1$ki
07-31-2008, 06:19 AM
Hi People,

I am not a veteran coder so this may be really obvious when I post that I can't get this to work. The page is at http://www.ewanski-gbc.com/contact2.php

and here is my code:




<?php include 'head1.php'; ?>
<title>Ewanski Green Building Consulting - Contact Us</title>
<script type="text/javascript">
<!--

function validate_form ( )
{
valid = true;
if(document.theform.fullname.value == ""){
alert ( "Please fill in your full name." );
valid = false;
}
if(document.theform.email.value == ""){
alert ( "Please fill in your email address." );
valid = false;
}
if(Verify_Email_Address(document.theform.email.value)){
alert ( "Please enter a valid email address.");
valid = false;
}
if(document.theform.message.value == ""){
alert ( "Please fill in a message." );
valid = false;
}
return valid;
}

function Verify_Email_Address($email_address)
{

//Assumes that valid email addresses consist of user_name@domain.tld
$at = strrpos($email_address, "@");
$dot = strrpos($email_address, ".");
alert ("at = " . $at);
if($at == false ||
$dot == false ||
$dot <= $at + 1 ||
$dot == 0 ||
$dot == strlen($email_address) - 1)
return(false);

$user_name = substr($email_address, 0, $at);
$domain_name = substr($email_address, $at + 1, strlen($email_address));

if(Validate_String($user_name) === false ||
Validate_String($domain_name) === false)
return(false);

return(true);
}
//-->
</script>
<?php include 'head2.php'; ?>

<div id="page">
<div id="content">
<div>
<h2 class="title">Contact Us</h2>
<p>If you have questions, feedback, or an interest in our services, please don't hesitate to contact us. We would love to hear from you!</p>
<p>Please contact us using the online web form below.</p>

<form method="post" action="formtoemail.php" name="theform" onsubmit="return validate_form ( );">
<table border="0" width="100%">
<tr>
<td>Full Name:</td>
<td><input type="text" name="fullname" size="40" maxlength=""></td>
</tr>

<tr>
<td>Email Address:</td>
<td><input type="text" name="email" size="40" maxlength=""></td>
</tr>

<tr>
<td>How We May Help You:</td>
<td><textarea name="message" rows="10" cols="50"></textarea></td>
</tr>

<tr>
<td></td>
<td><input type="submit" value="Send">
<input type="reset" value="Clear"></td>

</table>
</form>
</div>
</div>



<?php include 'footer.php'; ?>


I am confused. I just want it to validate that there is a real email in there. I checked the javascript console in Firefox and it said that strrpos wasn't valid but I looked it up at php.net and it's in there. So, I don't know where to look next.

Thanks in advance,
Patricia

Philip M
07-31-2008, 07:12 AM
Why do you not simplify things and cut out the clumsy email verification function. Replace:-


if(document.theform.email.value == "") {
alert ( "Please fill in your email address." );
valid = false;
}
if(Verify_Email_Address(document.theform.email.value)){
alert ( "Please enter a valid email address.");
valid = false;
}
By:-


var em = document.theform.email.value;
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {
alert ( "Please fill in a valid email address." );
valid = false;
}



BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

e1$ki
07-31-2008, 07:24 AM
Philip,

Thank you! the code works! Much simpler. And it works.

Yes, you have a good point about the "thanks in advance" thing. I didn't mean for it to sound snotty but I think you are right. It does sound pretty bad. Thanks for taking the time to let me know.

Best,
Patricia

Philip M
07-31-2008, 07:29 AM
Patricia -

Glad it works and welcome to the forum!

Don't take my comments about "thanks in advance" too seriously. But the fact is 98% of people who use that gruesome expression are newcomers to the forum.

Best wishes,

Philip M



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum