简体   繁体   中英

Email check with jQuery doesn't work

I tried to make Ajax form and tried to make email verification there. I found this solution 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.

In your logic that's where the email is wrong could that be the problem?

This is because your passing email.value . jquery objects don't have a parameter called value , so this will resolve as undefined .

.test() returns true if it is passed undefined, so your test will always pass.

use .val() instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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