【问题标题】:Cant get email validation working in Jquery无法在 Jquery 中进行电子邮件验证
【发布时间】:2012-06-13 14:54:09
【问题描述】:

大家好,我的 jQuery 有点问题,我的所有输入验证都正常工作,但我无法让我的 REGEX 代码验证我的电子邮件。尝试输入“hello@mywebsite.co.uk”我的个人电子邮件和我的工作电子邮件,它们都不起作用。

非常感谢任何帮助。

var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  $('.submitbutton').click(function () {
    if ($("input.name").val() == '') {
        alert("Name is a required field");
        return false;
    }
    else if ($("input.email").val() == '') {
        alert("Email is a required field");
        return false;
    }
    else if (!regex.test("input.email")) {
        alert("This is not a valid email address");
        return false;
    }
    else if ($("textarea.message").val() == '') {
        alert("Message is a required field");
        return false;
    }
    else {
        return true;
    }
});

干杯

【问题讨论】:

标签: jquery html validation email


【解决方案1】:

regex.test("input.email") 中,您正在测试实际字符串"input.email" 是否与正则表达式匹配。相反,您应该测试input.email 元素的

所以使用:

regex.test($("input.email").val())

【讨论】:

    猜你喜欢
    • 2011-01-31
    • 1970-01-01
    • 2015-08-26
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多