Jump to content
xisto Community
Sign in to follow this  
blazedupthug

Simple E-mail Validation check for correct address and syntax

Recommended Posts

Hey , this tutorial will tell you a very simple way to check if email addresses entered are with the correct syntax. Hope this will help you eliminate jokers. So this script will...

 

(check the characters, and check if there is a " someone[at]somewhere.extension ".

 

 

Ok, so firstly we create a function with all the invalid stuff in it:-

function CheckMail($email) {if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$", $email)) { return true;}else { return false; }}

Now, to check an email you just run the variable with the email in it through the function as follows:-

 

if ((empty($email)) || (!CheckMail($email]))){Die("Please go back and put in a valid e-mail adress!");}

Alright people first part checks the field has data in it, and the second part uses the function for the characters.

 

 

 

CheckMail shall return false if the email is incorrect and the script stops executing & echos an error message.

 

 

 

Hope that helps you! ;)

 

Written by blazedupthug ;)

Notice from jipman:

CODE-tags

Moved to programming > scripting > php


Share this post


Link to post
Share on other sites

I'm really sorry, but why should I worry about any kind of validation if there are a lot of php classes that do basic validations like this one, and much more? B)

rc_FormValidatior

I've looked over the web for the best validation PHP script and this is not the most complete, but the easiest and the most flexible I think, and that's why it's my favorite :P

don't re invent the wheel :blink:

Share this post


Link to post
Share on other sites

I don't think there's anything wrong with reinventing the wheel if it has modifications to it. I have seen a lot of validation classes and instead of being as complete as they could be, they opt for an easy method and check the basics, they then rely on another method (usually confirmation email) to check if that address really does exist. If there validation checked it as efficiently as it could be, it would save them the hassle of even sending the email, having it rejected/returned because the address did not exist.

if (eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$", $email))

I honestly believe I have seen this code before.

If we follow the standards set out by the RFC, there are quite a few things we aren't making sure that we do in this validation, most notable is this script allows the beginning of an email address to be a number. That's one flaw. It has limited the length of the end of an email. It may or may not work with archaeologist@someplace.musuem which could be a valid email address. It does not allow IPv4 or IPv6 addresses for domain name which are also valid yet RFC recommends using Fully Qualified Domain Names (FQDN).

I should probably update my email validation script that I did on here with my one that I did to meet the majority of what I found in the RFC on the subject.

To make use of an email validation you should also use ECMAScript (Javascript) to validate it first and then PHP server side to validate it (double check). At least with ECMAScript you can notify the user that it's incorrect before making them submit the form saving time.


Cheers,


MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.