Regular expression for a specific domain name

I am regex noob, but I want to write a regular expression for checking email for the domain name xyz.com.it, if the user key is on abc.com or other domain domain names, it will pass. If the user keys in xyz are after @, then only xyz.com.it will pass, others, such as xyz.net.it or xyz.net, will not pass. Any idea how to do this?

I tried

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

which only checks regular email

+5
source share
1 answer

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

 $email = " xyz@xyz.com "; $email2 = " xyz@xyz.net "; $valid_domain = "@xyz.com"; function checkValidDomain($email, $valid_domain){ if(!filter_var($email,FILTER_VALIDATE_EMAIL) !== false){ if(strstr($email,"@") == $valid_domain){ return "Valid"; }else{ return "Invalid"; } }else{ return "Invalid Email"; } } echo checkValidDomain($email, $valid_domain);// Valid echo checkValidDomain($email2, $valid_domain);// Invalid 

Why I didnโ€™t use regex here, you can read many of these streams in SO too Email Authentication Using Regular Expression in PHP and Using Regular Expression to Verify Email Address

0
source

Source: https://habr.com/ru/post/1247330/


All Articles