Jump to content

Regex For A Specific Email Address "@shrewsbury.ac.uk"


jbonnett

Recommended Posts

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.....

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 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.