2013-05-10 111 views
0

我有一個jQuery來驗證我的電子郵件ID,但驗證後提交表單而不是阻止它。jquery郵件驗證提交表格

我的代碼是

$(document).ready(function(e) { 
    $('#personaledits').click(function(e) { 
     $('#persemail').each(function(e) { 
      email_address = $(this); 
      email_regex = /^[a-zA-Z0-9._-][email protected][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; } 
     }); 
    }); 
}); 

回答

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._-][email protected][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; 
      } 
     }); 
    }); 
}); 
+0

謝謝你的男人,他能很好 – 2013-05-13 10:19:44

+0

高興它幫助! – 2013-05-13 10:20:19

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); 
});