jquery邮件验证提交表单

时间:2013-05-10 06:51:26

标签: php jquery email-validation

我有一个jquery来验证我的电子邮件ID,但在验证后它会提交表单而不是阻止它。

我的代码是

$(document).ready(function(e) {
    $('#personaledits').click(function(e) {
        $('#persemail').each(function(e) {
            email_address = $(this);
            email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
            if(!email_regex.test(email_address.val())){  alert('Please enter valid email'); e3.preventDefault(); return e.preventDefault(); return false; } 
        });
    });
});

2 个答案:

答案 0 :(得分:1)

试试这个:

$(document).ready(function () {  // event is not required here
    $('#personaledits').click(function (e) {
        $('#persemail').each(function () {
            email_address = $(this);
            email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
            if (!email_regex.test(email_address.val())) {
                alert('Please enter valid email');
                e.preventDefault();    // You need to prevent the click event here
                return false;
            }
        });
    });
});

答案 1 :(得分:0)

试试这个

$('#persemail').each(function(e) {
  email_address = $(this);
  email_regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
 });