jbonnett Posted September 25, 2012 Share Posted September 25, 2012 I'm hoping someone can help me I only want to accept "@shrewsbury.ac.uk" email addresses so a full email would be something like "[email protected]" with regex I know my normal email regex one is $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; I'm rubbish with regex so please help me..... Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/ Share on other sites More sharing options...
premiso Posted September 25, 2012 Share Posted September 25, 2012 I don't think you would want to use regex in the way you intended, since we know what the domain portion needs to be let's match that exactly. So I would check that the string ends in the @shrewbury.ac.uk and then use the built in email filter, filter_var to make sure the email is valid. <?php if (preg_match('#.*@shrewsbury.ac.uk$#i', $email) && filter_var($email, FILTER_VALIDATE_EMAIL) !== false) { echo 'Valid email, yay!'; }else { echo 'Invalid email boo!'; } Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380885 Share on other sites More sharing options...
jbonnett Posted September 25, 2012 Author Share Posted September 25, 2012 I don't think you would want to use regex in the way you intended, since we know what the domain portion needs to be let's match that exactly. So I would check that the string ends in the @shrewbury.ac.uk and then use the built in email filter, filter_var to make sure the email is valid. <?php if (preg_match('#.*@shrewsbury.ac.uk$#i', $email) && filter_var($email, FILTER_VALIDATE_EMAIL) !== false) { echo 'Valid email, yay!'; }else { echo 'Invalid email boo!'; } I have to integrate it with the old code. Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380888 Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 And? Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380889 Share on other sites More sharing options...
premiso Posted September 25, 2012 Share Posted September 25, 2012 <?php $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@shrewsbury.ac.uk$"; Ok. Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380892 Share on other sites More sharing options...
jbonnett Posted September 25, 2012 Author Share Posted September 25, 2012 Thank you work perfectly Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380907 Share on other sites More sharing options...
Christian F. Posted September 25, 2012 Share Posted September 25, 2012 You need to escape the periods that are a part of the domain, otherwise they'll be read as a RegExp meta character (match all except newlines). Link to comment https://forums.phpfreaks.com/topic/268789-regex-for-a-specific-email-address-shrewsburyacuk/#findComment-1380917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.