Forum Moderators: coopster

Message Too Old, No Replies

email validation

email validation on conatct form

         

benji

1:46 pm on May 26, 2009 (gmt 0)

10+ Year Member



I have a contact form with both Javascript and php. The Javascript I'm OK with, but struggle with php!

What i really want is validation for either a blank entry (the way I see it, just because someone is browsing the web, it doesn't mean they have got an email address), or valid email.

So far it validates the email OK, but I don't know how to set it to also accept a blank entry.

Here is the relevant code from the script:

$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

All I would like to know is what do I put and where do i put it?
Thanks in advance

coopster

1:52 pm on May 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Only check the validity if a non-empty value exists. Something like ...
if ($email_from && !eregi($email_exp,$email_from)) {

Also, you may consider using the perl compatible regular expressions. Future versions of PHP seem to be moving away from the POSIX regular expression engine.