Simple Email Validation PHP ScriptsHere is a simple example of an email validation function:You would call this email validation function within your php script like this:
if (validemail($email_provided)) {
print "<b>Success! The email provided looks good! </b><br>
We can now store this, send off an email, or whatever
else we needed to do with the email supplied.";
} else {
print "Sorry the email supplied does not match our criteria.";
}
So now lets try this out:This script is a typical simple method to verify the email address information supplied, appears to be in a format we want to accept. To explain the function itself, we are using the php eregi function and matching someone@somewhere.tld. Explained a bit further the someone is allowed any letters in upper and lower case, numbers, _ (underscores), - (dashes) and . (dot/period). We then match the at sign @ and proceed to match a properly formatted domain name. A valid domain name, or hostname consists of letters, numbers, dashes, dots/periods, followed by the domain tld extension. The domain tld extension can be 2 to 4 characters. We do not have to worry or differentiate tlds that have more than one dot, as the hostname itself can be subdomain.domain.com, so for this purpose, we are only concerned the string ends in a dot and 2 to 4 characters. A note that the four characters is a bit new with the new .info tld. To take this function further to actually match a currently functioning domain name, you could then use some networking functions that could let you know the domain is up and running, as well even go as far to check the hostname has current mx (mail exchange) records. Another addition to this function could be to allow user@ip_address which could read user@192.168.0.1 or any 4 sets of digits with 1 to 3 digits each, that each do not exceed the number 255, which is the highest number used in ip addresses. A quick function using regular expression to test a valid ip address could look like this: There are many cases where wanting to check for formatting is needed. The best way to know if an email address is actually working and that there is an actual live person on the other end, is to simply send them an email, asking them to respond, or go to some page you have with a special code that verifies they got your message and instructions. Often this level of verification is not required, but wanting to know the person entered in what appears to be correct information, is a good way to insure your script is doing what it set out to do. Please feel free to use these examples and if you find yourself in need of assistance, zubit.com is in the business of providing quality web application development and would be very happy to provide you with a quote. Back to main php scripts list |