ronbryce Posted January 20, 2016 Share Posted January 20, 2016 Pls, how do I make my registration page accept only one email address domain. like john@test.com, peter@test.com? Quote Link to comment https://forums.phpfreaks.com/topic/300576-php-restrict-emails/ Share on other sites More sharing options...
QuickOldCar Posted January 20, 2016 Share Posted January 20, 2016 (edited) Would be easier for us to see the code you are using now. You can use the email filter <?php $email = "john@test.com, peter@test.com"; if(filter_var($email, FILTER_VALIDATE_EMAIL)){ echo $email; }else{ echo $email." Failed"; } ?> Edited January 20, 2016 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/300576-php-restrict-emails/#findComment-1530125 Share on other sites More sharing options...
QuickOldCar Posted January 20, 2016 Share Posted January 20, 2016 (edited) If you want to use just the first one can do something like this. <?php $email = "john@test.com, peter@test.com"; if(strstr($email, ',')){ $exploded = explode(',',$email); $email = trim($exploded[0]); } if(filter_var($email, FILTER_VALIDATE_EMAIL)){ echo $email; }else{ echo $email." Failed"; } ?> Edited January 20, 2016 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/300576-php-restrict-emails/#findComment-1530127 Share on other sites More sharing options...
QuickOldCar Posted January 20, 2016 Share Posted January 20, 2016 Don't forget to trim() the input, will fail the validation due to whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/300576-php-restrict-emails/#findComment-1530128 Share on other sites More sharing options...
Jacques1 Posted January 20, 2016 Share Posted January 20, 2016 I think he wants to specifically restrict the domain of the addresses. Of course the obvious approach would be to simply split at the “@” sign. Note, however, that you need to split at the rightmost “@”, because this symbol is also allowed as a literal character within the local part. For example, foo\@bar@example.com is a valid address. <?php $address = 'foo@example.com'; $host = substr(strrchr($address, '@'), 1); var_dump( $host ); Alternatively, you could use an actual parser library. There are plenty on GitHub. Quote Link to comment https://forums.phpfreaks.com/topic/300576-php-restrict-emails/#findComment-1530132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.