Email Validation in PHP

Submitted by: 
Language: 
Visitors have accessed this post 9597 times.

Hi Guys,

This code is an email validator. It will validate the email address that you input. If the email address is not valid, like it don’t have an @ symbol, then the script will display a message that the email is invalid.

This script is useful if you have a user registration page.

Sample code:

  1. <?php
  2. include('header.php');
  3. ?>
  4. <html>
  5. <body>
  6. <div class="container">
  7. <form class="form-horizontal" method="POST">
  8. <div class="control-group">
  9. <label class="control-label" for="inputEmail">Email</label>
  10. <div class="controls">
  11. <input type="text" name="email" id="inputEmail" placeholder="Email" required>
  12. </div>
  13. </div>
  14. <div class="control-group">
  15. <div class="controls">
  16. <button type="submit" name="submit" class="btn">Submit</button>
  17. </div>
  18. </div>
  19. <div class="control-group">
  20. <div class="controls">
  21. <?php
  22. if (isset($_POST['submit'])){
  23.  
  24. $email=$_POST['email'];
  25. $pattern = "/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i";
  26. //Input Validations
  27. if (!preg_match($pattern,$email)){
  28. ?>
  29. <div class="alert alert-danger span2">
  30. <?php echo 'Invalid Email Address'; ?>
  31. </div>
  32. <?php
  33. }else{
  34. ?>
  35. <div class="alert alert-success span3">
  36. <?php echo 'This is a Valid Email Address'; ?>
  37. </div>
  38. <?php
  39. }
  40. }
  41. ?>
  42. </div>
  43. </div>
  44. </form>
  45. </div>
  46. </body>
  47. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.