Sportbob Posted June 26, 2008 Share Posted June 26, 2008 Hello, I would like to limit a form email field to only send to two domains. The below code works great for one domain (domain1), but what if I also want to allow users to send to (domain2) as well? Thanks for looking! if(empty($var)) { //do nothing } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+domain1.edu", $var)) { die ("<p>Error - please go back and enter a valid domain1 email address. Form does not allow EPHI sent outside of domain1.</p>"); } Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/ Share on other sites More sharing options...
KevinM1 Posted June 26, 2008 Share Posted June 26, 2008 I believe you can just put a | between the two domains: (domain1.edu | domain2.edu) That should tell it to accept either domain1 or domain2. You can test regular expressions here: http://regexlib.com/RETester.aspx Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575439 Share on other sites More sharing options...
Wolphie Posted June 26, 2008 Share Posted June 26, 2008 Yeah, I would agree with Nightslyr, using a complex regular expression wouldn't be necessary for a definite amount of domains allowed. You should just use a wild card before the domain, or check to see if the domain even exists in the string. Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575444 Share on other sites More sharing options...
Sportbob Posted June 26, 2008 Author Share Posted June 26, 2008 Thank you for the reply's. (domain1.edu | domain2.edu) returned the die error, as did [domain1.edu | domain2.edu] or [domain1 | domain2].edu. I will continue looking into alternative solutions. Thanks again. Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575452 Share on other sites More sharing options...
Wolphie Posted June 26, 2008 Share Posted June 26, 2008 It should be something like if((!strpos($email, 'domain1.edu')) || (!strpos($email, 'domain2.edu'))) { die('Please enter a valid e-mail address.'); } else { // do something else... } Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575471 Share on other sites More sharing options...
roopurt18 Posted June 26, 2008 Share Posted June 26, 2008 I notice you're using ereg where I normally use preg, so perhaps the rules are different. But you have domain.edu in your regexp. That dot does not match a dot, it matches anything. What you want is: domain[.]edu or domain\.edu Also, if you want multiple domains, try changing +domain1.edu to: +(domain1)|(domain2)[.]edu Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575474 Share on other sites More sharing options...
Sportbob Posted June 26, 2008 Author Share Posted June 26, 2008 Thank you all very much for your replies. I was able to resolve the issue with roopurt18's suggestion! Thank you! Final version below. if(empty($var)) { //do nothing } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+(domain1)|(domain2)[.]edu", $var)) { die ("<p>Error - please go back and enter a valid email address. Form does not allow EPHI sent outside of domain1 or domain2</p>"); } Link to comment https://forums.phpfreaks.com/topic/112089-solved-help-with-validating-form-email-addresses/#findComment-575576 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.