
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to validate an email address in PHP ?n
In this article, we will learn to validate an email with PHP regular expression. We will learn different methods to validate email address in PHP.
Method1
The function preg_match() checks the input matching with patterns using regular expressions.
Example
<?php function checkemail($str) { return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; } if(!checkemail("alex@tutorialspoint.com")){ echo "Invalid email address."; } else{ echo "Valid email address."; } ?>
Output
Valid email address.
In the above example PHP preg_match() function has been used to search string for a pattern and PHP ternary operator has been used to return the true or false value based on the preg_match return.
Method 2
We will discuss email validation using filter_var() method.
Example
<?php $email = "pattrick@tutorialspoint.com"; // Validate email if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo("$email is a valid email address"); } else{ echo("$email is not a valid email address"); } ?>
Output
pattrick@tutorialspoint.com is a valid email address
- Related Articles
- How to validate an email address in C#?
- How to validate email address in JavaScript?
- Validate Email address in Java
- How to validate an email address using Java regular expressions.
- Python program to validate email address
- How to validate email address using RegExp in JavaScript?
- How to validate Email Address in Android on EditText using Kotlin?
- How to validate email using jQuery?
- C Program to validate an IP address
- How to validate an email id using regular expression in Python?
- How to validate URL address in JavaScript?
- How should I validate an e-mail address in Android?
- How to validate the IP address using PowerShell?
- Validate IP Address in C#
- Validate IP Address in Python

Advertisements