Regex for Validating Custom Email Domains in PHP - implementing Whitelist Logic
I've spent hours debugging this and I've searched everywhere and can't find a clear answer... I'm trying to validate email addresses in PHP but with a specific requirement: I need to ensure that the email domain is from a predefined whitelist. For example, I want to accept emails from `example.com`, `mydomain.org`, and `testsite.net` only. However, I'm running into issues with my current regex pattern. It seems to allow domains that are not on my list. Hereโs the regex pattern Iโm using: ```php $pattern = '/^[a-zA-Z0-9._%+-]+@(example\.com|mydomain\.org|testsite\.net)$/'; ``` I've tested it with the following code: ```php $emails = [ 'user@example.com', 'user@mydomain.org', 'user@testsite.net', 'user@notallowed.com', 'user@mydomain.org.uk', ]; foreach ($emails as $email) { if (preg_match($pattern, $email)) { echo "$email is valid.\n"; } else { echo "$email is invalid.\n"; } } ``` When I run this, I get valid for both `user@mydomain.org` and `user@mydomain.org.uk`, which is incorrect because the latter isn't in my whitelist. I suspect thereโs an scenario with how Iโm escaping the dots or the grouping. Can someone help clarify why this regex isn't working as intended and suggest how I can fix it? Thanks! My development environment is Windows. Any examples would be super helpful. The stack includes Php and several other technologies. What am I doing wrong?