使用 jQuery 的电子邮件检查不起作用

Email check with jQuery doesn't work

我尝试制作 Ajax 表格并尝试在那里进行电子邮件验证。我找到了这个解决方案 http://jsfiddle.net/EFfCa/

但无法在我的脚本中打开它:

<script> 
    $('#joinForm').ajaxForm(function() {
        var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
        var name = $("input[name=name]")
        var email = $("input[name=email]")
        if(name.val()==''||email.val()=='') {
            $(".notify").show();
            $(".notify p").text('empty');
        } else if(testEmail.test(email.value)) {
            $(".notify").show();
            $(".notify p").text('email is wrong');      
        } else {
            $(".notify").show();
            $(".notify p").text('good');    
        }
    }); 
</script>

表单总是通过验证,即使电子邮件是错误的。验证空字段效果很好...

  $('input').blur(function() {
        var testEmail =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if (testEmail.test(this.value)) alert('passed');
        else alert('failed');
    });

如果电子邮件正确,下行 else if(testEmail.test(email.value)) 将 return 为真。

按照您的逻辑,这就是电子邮件错误的地方,这可能是问题所在吗?

这是因为你的传球email.valuejquery 对象没有名为 value 的参数,因此这将解析为 undefined

.test() returns 如果通过未定义则为真,因此您的测试将始终通过。

改用.val()