【问题标题】:Validate email with a regex in jQuery在 jQuery 中使用正则表达式验证电子邮件
【发布时间】:2020-12-23 10:02:23
【问题描述】:

参考这个问题:

How can I set a minimum length for a field with jQuery?,

<form id="new_invitation" class="new_invitation" method="post" data-remote="true" action="/invitations" accept-charset="UTF-8">
    <div id="invitation_form_recipients">
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_0"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_1"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_2"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_3"><br>
    </div>
    <input type="submit" value="Send invitation" name="commit">
</form>

使用 jQuery 为字段设置最小长度的代码是什么?

$('#new_invitation').submit(function(event) {
    if ($('#invitation_form_recipients input').filter(function() {
        return $(this).val();
    }).length == 0) {
        // All the fields are empty
        // Show error message here

        // This blocks the form from submitting
        event.preventDefault();
    }
});

如何使用 jQuery 验证每个字段输入是否具有有效的电子邮件地址?在上面的代码中?


解决方案!

$('#new_invitation').submit(function(e) { $('#invitation_form_recipients input').each(function(e) { email_address = $(this); email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i; if(!email_regex.test(email_address.val())){ alert('这不是有效的电子邮件'); e.preventDefault();返回假; } }); });

【问题讨论】:

标签: jquery


【解决方案1】:

您可能想使用像described here 这样的正则表达式来检查格式。提交表单后,对每个字段运行以下测试:

var userinput = $(this).val();
var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

if(!pattern.test(userinput))
{
  alert('not a valid e-mail address');
}​

【讨论】:

  • 我正在尝试这个,但它不起作用,我刚刚写了-abc@gmail.com。 &lt;input type="text" class="form-control" id="manager_email" placeholder="Email Address" value="" pattern="/^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i" required /&gt; 任何人都对此有所了解。
  • 如果您使用输入 pattern,请确保不要使用 surround it with forward slashes(您可能需要手动将小写字符添加到模式中以允许不区分大小写的输入)。 pattern="^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
  • 上述模式不验证 RFC5322 中指定的正确电子邮件地址,并且具有误导性。如果您在实时网站上使用它,您将阻止有效用户。地址的本地部分允许使用特殊字符!#$%&'*+-/=?^_`{|}~ 请参阅stackoverflow.com/questions/2049502/…tools.ietf.org/html/rfc5322
【解决方案2】:

这个正则表达式可以帮助您根据 gmail.com 使用的所有标准检查您的电子邮件地址。

var re = /^\w+([-+.'][^\s]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

var emailFormat = re.test($("#email").val()); // This return result in Boolean type

if (emailFormat) {}

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
  • @DimaSan 感谢您的宝贵意见。
【解决方案3】:
function mailValidation(val) {
    var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    if (!expr.test(val)) {
        $('#errEmail').text('Please enter valid email.');
    }
    else {
        $('#errEmail').hide();
    }
}

【讨论】:

  • 正如 Rick Fox 在其他 cmets 中所指出的,_ 和许多其他奇怪的字符在 @ 字符之前是允许的。实际上,_ 相当普遍。
【解决方案4】:
Email: {
                      group: '.col-sm-3',
                      enabled: false,
                      validators: {
                          //emailAddress: {
                          //    message: 'Email not Valid'
                          //},
                          regexp: {
                              regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
                              message: 'Email not Valid'
                          },
                      }
                  },

【讨论】:

    【解决方案5】:

    这:/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i 不适用于以下 Gmail 案例

    gmail.@gmail.com gmail@.gmail.com

    以下正则表达式将涵盖所有电子邮件点:我已经尝试了所有可能的点,并且我的测试用例也因为以下正则表达式而通过

    我从这个 URL 找到了这个解决方案:

    Regex Solution link

    /(?:((?:[\w-]+(?:\.[\w-]+)*)@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[a-z]{2,6}(?:\.[a-z]{2})?));*)/g
    

    【讨论】:

      【解决方案6】:

      这个:

       var email = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
      

      【讨论】:

      • 这匹配 %%%+@..aa 和类似的非地址。
      • 为什么这里比其他答案更重要?
      猜你喜欢
      • 2012-09-25
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多