jQuery 电子邮件验证在 Backspace 上失败

发布于 2024-12-17 23:24:41 字数 725 浏览 5 评论 0原文

function validate_email() {
    atsplit = $('input[name=email]').val().split('@');
    dotsplit = atsplit[1].split('.');
    if (atsplit.length == 2 && atsplit[0].length > 0 && dotsplit[dotsplit.length-1].length > 0 && dotsplit[dotsplit.length-2].length > 0) {
        $('#email').html('✔');
        valid_email = true; 
    }
    else {
        $('#email').html('');
        valid_email = false;
    }
}
$('*').live('change click focusout keyup submit', function(){
    validate_email();
});

我正在尝试验证电子邮件地址。如果电子邮件有效,#email 范围内会出现一个复选标记。除非您突出显示输入文本并按退格键(或者按住退格键以便一次击键即可删除输入),否则一切都会正常运行。在这些情况下,无论触发什么事件(单击、焦点移出等),复选标记都会保留。所有浏览器中的行为似乎都是相同的。任何想法有什么问题吗?

function validate_email() {
    atsplit = $('input[name=email]').val().split('@');
    dotsplit = atsplit[1].split('.');
    if (atsplit.length == 2 && atsplit[0].length > 0 && dotsplit[dotsplit.length-1].length > 0 && dotsplit[dotsplit.length-2].length > 0) {
        $('#email').html('✔');
        valid_email = true; 
    }
    else {
        $('#email').html('');
        valid_email = false;
    }
}
$('*').live('change click focusout keyup submit', function(){
    validate_email();
});

I'm trying to validate an email address. If the email is valid, a check mark is placed in the #email span. Everything works perfectly unless you highlight the input text and press the backspace key (or if you hold the backspace key so that the input is deleted in one keystroke). In these cases, the check mark remains regardless of what events are triggered (click, focusout, etc.). The behavior seems to be the same in all browsers. Any ideas what's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2024-12-24 23:24:41

试试这个 http://jsfiddle.net/Pq5Ut/ 并告诉我是否对您有帮助。我知道我没有使用与您相同的电子邮件验证,但我觉得这可能是您想要的。

仅供参考,我正在使用的电子邮件正则表达式刚刚从 jQuery 验证插件中提取。

Try out this http://jsfiddle.net/Pq5Ut/ and let me know if that helps you out. I know I'm not using the same email validation that you are, but I feel it might be what you want.

FYI, the email regex I'm using was just pulled from the jQuery validation plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文