【问题标题】:JavaScript filtering email using validatorJavaScript 使用验证器过滤电子邮件
【发布时间】:2017-09-17 12:13:49
【问题描述】:

我想知道您是否可以接受电子邮件地址的任何扩展名。而不是特定的域名扩展名。

例如

foo@surrey.ac.uk

foo@survey.ac.uk

这是我的代码。这是可行的,但问题是如果您为电子邮件 ac.uk 添加了扩展名,它不会接受它。例如,如果我注册了一个电子邮件 foo@ac.uk,它可以工作,但如果你尝试为电子邮件添加扩展名,如 foo@surrey.ac.uk 或 foo@survey.ac.uk,它不接受它。我希望它添加任何扩展才能正常工作。谢谢你们!

//for custom email
jQuery.validator.addMethod('customemail', function(value, element) {
    var s=value;
    var split = s.split('@');
    var regex = /^([a-zA-Z0-9_.+-])+$/;
    var s2="ac.uk";                  //The split array is the domain excluding the @
    var optionalValue = this.optional(element); //This is how other methods in alternativeMethods.js Validator handle this.

    //Debugging - This is useful to see visually what is happening
    //alert(split[0]);  // Shows the inputted username i.e chris or smokey
    //alert(split[1]);  // Shows the inputted domain
    //alert(regex.test(split[0]));  //Shows unfilled inputs problem or bad characters, true if good, false if bad
    //alert(s2 == split[1]);**  // Shows if the inputted domain matches variable s2, if it does we get a true

    if (optionalValue) {
        return optionalValue;
    }
    if(regex.test(split[0]) && (s2 == split[1]))  // has to be == not equals
    {
        return true;
    }
    else
    {
        return false;
    }
}, 'Please specify a Verified Email');

有人将此查询报告为与此帖子 Email validation using jQuery 重复的查询

但我的问题有所不同,因为我需要一个经过批准的域扩展。不是一般的电子邮件验证

【问题讨论】:

  • 你好@Jim 我认为这不是一个重复的问题。我在问一个需要不同域添加扩展的特定问题。我也不擅长java脚本,因为我还是个学生。但如果你能帮我解决我的问题,我会很高兴。
  • 抱歉,乍一看似乎是一样的。这是部分匹配。

标签: javascript php jquery email


【解决方案1】:

首先,您应该在重复的帖子条目中使用验证正则表达式 - 它比您的要好得多,因为您包含 .+ 这意味着匹配任何一个或多个任何字符 - 这将允许很多无效通过电子邮件。您应该验证整封电子邮件,而不仅仅是其中的一部分。

至于匹配域,由于您要包含子域,请使用正则表达式来完全匹配域或以域结尾但在其前面有句点(any@ac.uk 有效,any@surrey .ac.uk 有效,但 any@surreyac.uk 或 any@google.com 不匹配)。

JSFiddle Example

//for custom email
jQuery.validator.addMethod('customemail', function(value, element) {
  var s=value;
  var split = s.split('@');
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var s2=/(^|\.)ac.uk$/; // Regex to ensure domain is either exactly "ac.uk" or ends with ".ac.uk"

  //Debugging - This is useful to see visually what is happening
  //alert(split[0]);  // Shows the inputted username i.e chris or smokey
  //alert(split[1]);  // Shows the inputted domain
  //alert(regex.test(split[0]));  //Shows unfilled inputs problem or bad characters, true if good, false if bad
  //alert(s2 == split[1]);**  // Shows if the inputted domain matches variable s2, if it does we get a true

  // Ensure entire email is valid and that it is or ends with the required domain
  if(regex.test(s) && (s2.test(split[1])))
  {
    return true;
  }
  else
  {
    return false;
  }
}, 'Please specify a Verified Email')

【讨论】:

  • 成功了!非常感谢您。我从您拥有的代码中复制了所有内容并且它有效。 :)
猜你喜欢
  • 1970-01-01
  • 2012-08-23
  • 2011-10-28
  • 2023-04-06
  • 2011-10-20
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
相关资源
最近更新 更多