首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于电子邮件验证的Regex查询,以便在php中也包括.ac.uk

问用于电子邮件验证的Regex查询,以便在php中也包括.ac.uk
EN

Stack Overflow用户
提问于 2020-02-24 12:45:48
回答 2查看 85关注 0票数 1

我已经在网上搜索和搜索过堆栈溢出,以获得一些指导,但没有什么与我的代码非常吻合。我正在使用PHP对我的用户注册表单进行一些验证,如下面所示,我还想检查用户注册的电子邮件是否以.ac.uk结尾

代码语言:javascript
复制
  // We check for any empty inputs. 
  if (empty($firstName) || empty($lastName) || empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {
    header("Location: ../signup.php?error=emptyfields&username=".$username."&email=".$email);
    exit();
  }
  // We check for an invalid username AND invalid e-mail.
  else if (!preg_match("/^[a-zA-Z0-9]*$/", $username) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../signup.php?error=invalidunamemail");
    exit();
  }
  // Check for an invalid username. In this case ONLY letters and numbers allowed.
  else if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
    header("Location: ../signup.php?error=invalidcharacters");
    exit();
  }

  // Check for minimum amount of characters in username.
  else if (strlen($username) <7 ) {
    header("Location: ../signup.php?error=invalidlengthuname");
    exit();
  }

  // Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
  else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../signup.php?error=invalidemail");
    exit();
  }
  // Check if the repeated password is NOT the same.
  else if ($password !== $passwordRepeat) {
    header("Location: ../signup.php?error=passwordcheck&uname");
    exit();
  }
  else {
// Login
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-24 13:11:05

您可以添加preg_match来检查,但如果条件1有效,则可以跳过第二个条件。这很奇怪,但是如果单独添加,它就能工作了。

代码语言:javascript
复制
  // Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
  if ( !filter_var($email, FILTER_VALIDATE_EMAIL)  &&  preg_match('/ac.uk$/', $email) ) {

      header("Location: ../signup.php?error=invalidemail");
      exit();
  } 

这个会有用的

代码语言:javascript
复制
  // Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
  if ( !filter_var($email, FILTER_VALIDATE_EMAIL)) {

      header("Location: ../signup.php?error=invalidemail");
      exit();
  } 
  if (!preg_match('/ac.uk$/', $email)) { 

      header("Location: ../signup.php?error=invalidemail");
      exit();

  }
票数 1
EN

Stack Overflow用户

发布于 2020-02-24 13:17:55

正则表达式到电子邮件Regex匹配特定域

代码语言:javascript
复制
^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(yourdomain)\.ac.uk$

演示

这里是更多的例子

UPDATE:在您的评论中,首先检查扩展,如果它是可接受的域,然后验证。

代码语言:javascript
复制
$email = 'test@domain.ac.uk';
$extention = pathinfo($email, PATHINFO_EXTENSION);
if($extension == ".ac.uk"){
   //Do your validation here 
   filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
   //Invalid extention Reject email
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60376245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档