We have to validate each E-mail input value to ensure that the given value is formatted correctly. There are 3 ways of email validation available, using which we can achieve this.
- HTML
- Javascript
- PHP
Here we are going to discuss about PHP email validation on the HTML forms, normally it will be useful for Login form and Registration form.
In PHP, we can validate the email address by following two methods.
- Using filter_var() function
- Using Regular Expression
Email Validation using filter_var()
filter_var()
is used to filter a variable with a specified filter variable. This function is supported by PHP version greater than 5.2.
Syntax:
1 |
mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] ) |
Where $variable
is the input string that we are going to validate.
For the email validation, we should use FILTER_VALIDATE_EMAIL
as the $filter
parameter. Consider the following example.
Example for email validation using filter_var() in Bootstrap 4.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php // Declare error and success variables $error_msg = ''; $success_msg = ''; if($_POST) { // Get the posted values $email = $_POST['email']; // Prevent XSS attacks $email = htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); if (filter_var($email, FILTER_VALIDATE_EMAIL)) { $success_msg = 'VALID'; } else { $error_msg = 'NOT VALID'; } } ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="/demo/bootstrap-4.1.2-dist/css/bootstrap.min.css"> <title>PHP Email Validation Example using filter_var</title> <meta name="description" content="PHP Email Validation Example"> <style> .email-validation { background-color: #EDEDED; margin-top:50px; padding-top: 10px; padding-bottom: 20px; border-radius: 15px; border-color:#d2d2d2; border-width: 5px; box-shadow:0 1px 0 #cfcfcf; } h1 { font-size: 30px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="email-validation col-md-6 offset-md-3"> <h1>PHP Email Validation Example - filter_var()</h1> <?php if($error_msg):?> <div class="alert alert-danger"><?php echo $error_msg;?></div> <?php endif;?> <?php if($success_msg):?> <div class="alert alert-success"><?php echo $success_msg;?></div> <?php endif;?> <form method="post" action=""> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="text" class="form-control" name="email" placeholder="Enter email"> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="/demo/bootstrap-4.1.2-dist/js/bootstrap.min.js"></script> </body> </html> |
Example explanation
- First you need to download bootstrap 4.1 from https://getbootstrap.com/docs/4.1/getting-started/download/
- In our example, we have extracted the downloaded ‘Source files’ under the folder ‘/demo/bootstrap-4.1.2-dist’ which contain 2 folders, they are
css and js
. - Now create the basic bootstrap template with simple form which have email input element.
- Here we have not used any validation, if you want to validate the email field then check our article of Contact form validation in Javascript.
- We have used some basic design like background color and alignment using
CSS
inside<style>
tag. - In top of the page, there is the PHP programming to handle the form request.
if($_POST)
is used to allow the PHP processor inside the block{}
, only when user submit the form.- We have used XSS prevention using
htmlspecialchars()
PHP function and assigned the output value into$email
variable. - Then we are validating the
$email
variable usingfilter_var()
PHP function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php // Declare error and success variables $error_msg = ''; $success_msg = ''; if($_POST) { // Get the posted values $email = $_POST['email']; // Prevent XSS attacks $email = htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); if (filter_var($email, FILTER_VALIDATE_EMAIL)) { $success_msg = 'VALID'; } else { $error_msg = 'NOT VALID'; } } ?> |
Email Validation using Regular Expression – preg_match()
If you want to use custom PHP function other than filter_var()
, then you can perform the custom function using regular expression of preg_match()
PHP function.
Syntax:
1 |
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) |
pattern |
This is the string variable, you have to give the regular expression pattern for email here. |
subject |
This is the string variable. It is the input variable and the pattern will be applied on this string. |
matches, flags and offset variable are optional. | |
matches |
This is the array variable. It will contain the output results. |
flags |
Default value is 0 and it will be any one of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL |
offset |
Default value is 0. The search will begin at 0th position normally, if you want at any other position then you need to mention the integer value here. |
Example for email validation using preg_match() in Bootstrap 4.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php // Declare error and success variables $error_msg = ''; $success_msg = ''; if($_POST) { // Get the posted values $email = $_POST['email']; // Prevent XSS attacks $email = htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); $pattern = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"; if (preg_match($pattern, $email)) { $success_msg = 'VALID'; } else { $error_msg = 'NOT VALID'; } } ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="/demo/bootstrap-4.1.2-dist/css/bootstrap.min.css"> <title>PHP Email Validation Example using regular expression</title> <meta name="description" content="PHP Email Validation Example"> <style> .email-validation { background-color: #EDEDED; margin-top:50px; padding-top: 10px; padding-bottom: 20px; border-radius: 15px; border-color:#d2d2d2; border-width: 5px; box-shadow:0 1px 0 #cfcfcf; } h1 { font-size: 30px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="email-validation col-md-6 offset-md-3"> <h1>PHP Email Validation Example - Regular Expression</h1> <?php if($error_msg):?> <div class="alert alert-danger"><?php echo $error_msg;?></div> <?php endif;?> <?php if($success_msg):?> <div class="alert alert-success"><?php echo $success_msg;?></div> <?php endif;?> <form method="post" action=""> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="text" class="form-control" name="email" placeholder="Enter email"> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="/demo/bootstrap-4.1.2-dist/js/bootstrap.min.js"></script> </body> </html> |
Example explanation
- The process same like Example for email validation using
filter_var()
in Bootstrap 4.1 - The PHP programming differs only at the beginning and rest of the process remains the same.
- Here we are validating email field using preg_match() PHP function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // Declare error and success variables $error_msg = ''; $success_msg = ''; if($_POST) { // Get the posted values $email = $_POST['email']; // Prevent XSS attacks $email = htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); $pattern = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"; if (preg_match($pattern, $email)) { $success_msg = 'VALID'; } else { $error_msg = 'NOT VALID'; } } ?> |
Any one of email validation you can use, that is depending on the programmer.