简体   繁体   English

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

[英]Email check with jQuery doesn't work

I tried to make Ajax form and tried to make email verification there. 我试图制作Ajax表单,并尝试在那里进行电子邮件验证。 I found this solution http://jsfiddle.net/EFfCa/ 我找到了这个解决方案http://jsfiddle.net/EFfCa/

but can't turn it on in my script: 但无法在我的脚本中将其打开:

<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>

The form always passed verification even email is wrong. 表单总是通过验证,即使电子邮件是错误的。 Verification for empty fields works good... 验证空白字段效果很好...

The following line else if(testEmail.test(email.value)) will return true if the email is correct. 如果电子邮件正确,则以下else if(testEmail.test(email.value))否则将返回true。

In your logic that's where the email is wrong could that be the problem? 按照您的逻辑,这是电子邮件错误的地方,这可能是问题所在吗?

This is because your passing email.value . 这是因为您传递的email.value jquery objects don't have a parameter called value , so this will resolve as undefined . jquery对象没有名为value的参数,因此它将解析为undefined

.test() returns true if it is passed undefined, so your test will always pass. .test()如果未定义通过则返回true,因此您的测试将始终通过。

use .val() instead. 使用.val()代替。

  $('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');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM