Restrict user registration to emails on a single domain

I need to allow user registrations only for those with email addresses on a single domain. I have tested multiple regular expressions and this one works well in a regex sandbox environment, but as soon as I put it into my functions.php file, it simply rejects all registrations - even email addresses on the correct domain (with the error message below, so it's definitely this conditional that's breaking it). Am I: a) actually doing something stupid in the regex, even tho the online regex testers say it's doing what I want? b) using a regex syntax that WordPress doesn't support? c) screwing up something in the rest of the function? d) misunderstanding the codex, and it's not as simple as just adding this to functions.php?

add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if (!preg_match('( |^)[^ ]+@mydomain\.co\.uk( |$)', $user_email )) {
    $errors-add( 'invalid_email', __( 'ERROR: Only valid "mydomain" email address is allowed.' ));
    $user_email = '';
    }

    return $errors;
}

BTW, I'm aware that the regular expression I've used doesn't fully test that a valid email address has been entered - all I need it to do is check that the domain is exactly right (and obviously this isn't the 'real' one!!)

I also tried the solution in a previous post (2011) but it simply broke my whole site :(

I've been staring at this so long now, it could be blindingly obvious and I wouldn't see it...

Many thanks!

Topic validation functions domain user-registration Wordpress

Category Web


You need to use delimiters in order for your regex to work.

From the link above

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

so in your case, you could have something like this. Notice how I used the / to delimit the regex

add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

  if (! preg_match('/( |^)[^ ]+@mydomain\.co\.uk( |$)/', $user_email )) {
    $errors->add( 'invalid_email', __( 'ERROR: Only valid "mydomain" email address is allowed.' ));
    $user_email = '';
  }

  return $errors;
}

I notice you allow white space at the beginning and the end of your regex.

Maybe you want to use this regex to avoid allowing whitespace (unless this is the desired outcome)

if (! preg_match('/^[a-zA-Z0-9._-]+@mydomain\.co\.uk$/', $user_email )) {...

  • ^ means starting from
  • [a-zA-Z0-9._-] means any alphanumerical caracter and dot . underscore _ and dash - characters are allowed
  • + means one or more of the preceding token, [] in this case
  • \. escapes the dot so it's not interpreted as any characters
  • $ means ending here

also note that since you filter into WP registration validation mechanism, you are adding to the current check, so technically, WP will check if this is a valid email on top of what you are asking

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.