【问题标题】:jquery mail validation submits the formjquery电子邮件验证提交表单
【发布时间】:2013-05-04 19:28:05
【问题描述】:

我有一个 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; } 
        });
    });
});

【问题讨论】:

    标签: php jquery email-validation


    【解决方案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;
                }
            });
        });
    });
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-27
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多