Page 1 of 1

PHP Email Form Validation

Posted: Thu Oct 02, 2008 6:59 pm
by reth
Hi Everyone,

I was hoping that someone can help me out. I made a form and want it to only make emails work in it (have to type blah@hotmail.com.) I am having issues with how to make it work. The code I have so far looks like this:

<div id="unsubscribe">

<form id="form" name="form" class="rt" method="post" action="">
<input type="hidden" name="source" value="flash games" />
<fieldset>
<label for="email1">
<span>Please enter your email address:</span>
<input type="text" name="email1" id="email1" />
</label>
<button class="submitBtn" name="button" id="button" type="submit"><span>Unsubscribe</span></button>
</fieldset>
</form>

</div>

Please let me know if any of you know the solution.

Thanks,
Reth

Re: PHP Email Form Validation

Posted: Fri Oct 03, 2008 12:57 am
by The_Anomaly
You have two options. Client-side validation (Javascript), or Server-side validation (PHP). The former can easily be bypassed, but is arguably easier to implement. The latter need to use REGEX, and can get a bit confusing.

Check this out.

Re: PHP Email Form Validation

Posted: Fri Oct 03, 2008 8:53 am
by ironhamster88
this is a valid e-mail regex pattern.

Code: Select all

$pattern = '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i';
 
if(preg_match($pattern, $_POST['email1'])) {
    // email address is valid - send email, whatever
} else {
    // incorrect email - display error message or something
}
hope it helps.