PHP Email Form Validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reth
Forum Newbie
Posts: 1
Joined: Thu Oct 02, 2008 6:58 pm

PHP Email Form Validation

Post 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
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: PHP Email Form Validation

Post 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.
User avatar
ironhamster88
Forum Newbie
Posts: 3
Joined: Fri Oct 03, 2008 8:51 am
Location: Essex, UK

Re: PHP Email Form Validation

Post 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.
Post Reply