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 "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 by jbonnett
Link to comment
Share on other sites

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 by premiso
Link to comment
Share on other sites

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 by jbonnett
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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