How to validate an Email using PHP?
Last Updated :
13 Feb, 2019
This article contains different methods to validate an email address in PHP. It uses regular expressions and inbuilt email validation function. The input string is taken from the user and matches it with the predefined regular expressions and if the regular expression and input string found to be matched than it returns true and proceed further.
Method 1: Email validation using regular expression.
php
<?php
// PHP program to validate email
// Function to validate email using regular expression
function email_validation($str) {
return (!preg_match(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str))
? FALSE : TRUE;
}
// Function call
if(!email_validation("[email protected]")) {
echo "Invalid email address.";
}
else {
echo "Valid email address.";
}
?>
Output:
Valid email address.
Explanation: In the above example, passing the email to the user defined function
email_validation( $email ), which use this example and matches with the regular expression by using the predefined function
preg_match(). This predefined function match the whole input with regular expression and returns True if match found otherwise returns False.
Method 2: Email validation using filter_var() method.
php
<?php
// Declare variable and initialize
// it to email
$email = "[email protected]";
// 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");
}
?>
Explanation: In the above example, passing the input email address to the predefined function
filter_var(), which takes two parameters as input email and second is type of email filter. This function filters the email and returns
true or
false.
Method 3: Email validation using FILTER_SANITIZE_EMAIL filter.
php
<?php
// Declare variable and store it to email
$email = "[email protected]";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// 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");
}
?>
Explanation: In the above example, use FILTER_SANITIZE_EMAIL filter to remove all unsupported characters and then use FILTER_VALIDATE_EMAIL filter to validate email.
Similar Reads
How to send an email using PHPMailer ? PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming. PHPMailer simplifies the p
3 min read
How to Validate Password using Regular Expressions in PHP ? Password validation is a crucial part of web application security. Regular expressions provide a powerful tool to define complex patterns. This article will guide you through various approaches to validate passwords using regular expressions in PHP. Approach 1: Basic Password ValidationIn this case,
1 min read
How to Insert Form Data into Database using PHP ? Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.Here, we wi
5 min read
How to Insert Form Data into Database using PHP ? Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.Here, we wi
5 min read
How to Insert Form Data into Database using PHP ? Inserting form data into a MySQL database using PHP allows web applications to store user inputs like registrations or feedback. By collecting and validating the data from HTML forms, PHP securely inserts it into the database, ensuring data integrity and protection against security risks.Here, we wi
5 min read
How to check form submission in PHP ? Given a form and the task is to check form submitted successfully or not. Use the PHP program for the backend to check form submission and HTML and CSS to create the form as frontend. Syntax: if (!empty($_POST)) if (isset($_POST['submit'])) Use these two statements to check whether the form submitte
3 min read