jbonnett Posted September 25, 2012 Share Posted September 25, 2012 (edited) 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 "user@shrewsbury.ac.uk" 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..... Edited September 25, 2012 by jbonnett Quote Link to comment Share on other sites More sharing options...
premiso Posted September 25, 2012 Share Posted September 25, 2012 (edited) 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!'; } Edited September 25, 2012 by premiso Quote Link to comment Share on other sites More sharing options...
jbonnett Posted September 25, 2012 Author Share Posted September 25, 2012 (edited) 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. Edited September 25, 2012 by jbonnett Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 And? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
jbonnett Posted September 25, 2012 Author Share Posted September 25, 2012 Thank you work perfectly Quote Link to comment 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). Quote Link to comment 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.