Welcome to this comprehensive PHP Email Validation tutorial! In this lesson, we'll explore how to validate emails using PHP, a popular server-side scripting language. By the end of this tutorial, you'll be able to validate emails effectively, ensuring only valid email addresses are accepted in your forms. Let's get started! π
Email validation is the process of verifying if an email address is well-formed and follows the standard email format. This helps in ensuring that the email address provided by the user is valid, reducing spam and maintaining the integrity of your user data.
Before we dive into the code, let's understand the basic email format:
user@example.com
Now, let's create a simple PHP function to validate emails.
function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}In the above code, we've created a function named isValidEmail() that takes an email address as an argument and uses the built-in filter_var() function with the FILTER_VALIDATE_EMAIL filter to validate the email address.
Let's test our function with some examples:
$valid_email = "john.doe@example.com";
$invalid_email1 = "john.doeexample.com";
$invalid_email2 = "johndoe@com";
echo "Valid Email: ";
if (isValidEmail($valid_email)) {
echo "β
" . $valid_email;
} else {
echo "β " . $valid_email . " is not a valid email.";
}
echo "\nInvalid Email 1: ";
if (isValidEmail($invalid_email1)) {
echo "β
" . $invalid_email1;
} else {
echo "β " . $invalid_email1 . " is not a valid email.";
}
echo "\nInvalid Email 2: ";
if (isValidEmail($invalid_email2)) {
echo "β
" . $invalid_email2;
} else {
echo "β " . $invalid_email2 . " is not a valid email.";
}In the above example, we've created three variables: a valid email, an invalid email without a domain, and an invalid email with an incorrect TLD. When we run this code, we'll see the following output:
Valid Email: β
john.doe@example.com
Invalid Email 1: β john.doeexample.com is not a valid email.
Invalid Email 2: β johndoe@com is not a valid email.
What does the `filter_var()` function with the `FILTER_VALIDATE_EMAIL` filter do in PHP?
While the basic email validation function we've created works well for simple cases, it may fail to validate more complex email addresses. To account for this, let's create a more robust email validation function that supports additional email address formats:
function isValidEmail($email) {
// Define email validation regex
$regex = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/";
// Use preg_match() to validate email
return (preg_match($regex, $email) === 1);
}In the above code, we've created a more complex email validation regex (regular expression) that supports additional email address formats, such as those with subdomains, local-part with periods, and domain names with periods in the third level.
Now, let's test our advanced email validation function with some additional examples:
$advanced_valid_email = "john.doe+test@example.com";
$advanced_invalid_email1 = "john.doe@examplecom";
$advanced_invalid_email2 = "johndoe@domain.co";
$advanced_invalid_email3 = "johndoe@example..com";
echo "Advanced Valid Email: ";
if (isValidEmail($advanced_valid_email)) {
echo "β
" . $advanced_valid_email;
} else {
echo "β " . $advanced_valid_email . " is not a valid email.";
}
echo "\nAdvanced Invalid Email 1: ";
if (isValidEmail($advanced_invalid_email1)) {
echo "β
" . $advanced_invalid_email1;
} else {
echo "β " . $advanced_invalid_email1 . " is not a valid email.";
}
echo "\nAdvanced Invalid Email 2: ";
if (isValidEmail($advanced_invalid_email2)) {
echo "β
" . $advanced_invalid_email2;
} else {
echo "β " . $advanced_invalid_email2 . " is not a valid email.";
}
echo "\nAdvanced Invalid Email 3: ";
if (isValidEmail($advanced_invalid_email3)) {
echo "β
" . $advanced_invalid_email3;
} else {
echo "β " . $advanced_invalid_email3 . " is not a valid email.";
}In the above example, we've created three additional variables: a valid email with a plus sign, an invalid email with an incorrect TLD, an invalid email with a domain name that is too short, and an invalid email with a domain name with two periods in the third level. When we run this code, we'll see the following output:
Advanced Valid Email: β
john.doe+test@example.com
Advanced Invalid Email 1: β john.doe@examplecom is not a valid email.
Advanced Invalid Email 2: β johndoe@domain.co is not a valid email.
Advanced Invalid Email 3: β johndoe@example..com is not a valid email.
What does the `preg_match()` function do in PHP?
That's it for our PHP Email Validation tutorial! You now have the knowledge to validate emails effectively using PHP, making your forms more robust and user-friendly. Happy coding! π
If you found this tutorial helpful, don't forget to share it with your friends and peers! π€ Stay tuned for more exciting PHP tutorials coming your way on CodeYourCraft! π