Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Email Validation in PHP Regular Expression

    PHP has different ways of email validation, and one of the most popular ways is to achieve this task by using a regular expression.

    This tutorial contains a PHP function to validate an email address. The function checks the input matching with patterns using regular expressions. 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 pre_match return.

    <?php
    
    function valid_email($str) {
            return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }
    
    if(!valid_email("[email protected]")){
            echo "Invalid email address.";
    }else{
            echo "Valid email address.";
    }
    
    ?>


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.