PHP Email Validation with Example

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:

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

Run above example

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 using filter_var() PHP function.

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:

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

Run above example

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.

Any one of email validation you can use, that is depending on the programmer.

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *