Excube Dzynes Excube Dzynes - 2 years ago 124
PHP Question

Regex for specific domain name

I am a regex noob but I wish to write a regex to check for email for domain name xyz.com.it if user key in abc.com or other TLD domain names, it will pass. If user keys in xyz after the @ then, only xyz.com.it will pass, others like xyz.net.it or xyz.net will not pass.Any idea how to do it?

I had tried

var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var regexEmail = regex.test($('#email').val());

that only validates normal email

Answer Source

Now instead of using regex you can simply use strstr function of PHP like as

$email = "xyz@xyz.com";
$email2 = "xyz@xyz.net";
$valid_domain = "@xyz.com";

function checkValidDomain($email, $valid_domain){
    $regex = "/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i";
        if(strstr($email,"@") == $valid_domain){
            return "Valid";
            return "Invalid";
        return "Invalid Email";

echo checkValidDomain($email, $valid_domain);// Valid
echo checkValidDomain($email2, $valid_domain);// Invalid


Note : Regex is been used from https://fightingforalostcause.net/content/misc/2006/compare-email-regex.php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download