2

我有一个表单和一个提交按钮。我想在这个提交按钮上用 jquery 设置一个条件。我不希望它仅在用户输入有效的电子邮件地址时才发布表单。

HTML 代码:

<form  method="post" action="{% url sportdub.views.login %}">
    <input type="text" name="email" value="" class="span8"/>
    <input type="submit"></button>
</form>

Javascript代码:

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

我想要做的是在我的提交按钮上设置一个条件:

if( !isValidEmailAddress( emailaddress ) ) { 
    /* don't submit the form and raise an error */ 
} else { 
    /* submit the form */
}

关于我如何做到这一点的任何想法?

4

3 回答 3

3

您需要将onsubmit事件处理程序添加到您的表单中,return false如果它没有通过验证。

例如根据您的代码:

  <form  method="post" action="{% url sportdub.views.login %}" onsubmit="return validateForm();">
    <input type="text" id="formEmail" name="email" value="" class="span8"/>
    <input type="submit"></button>
  </form>

在 JS 中:

function validateForm() {
    if( !isValidEmailAddress( document.getElementById( 'formEmail' ).value ) ) { 
        // also before return you can add alert( 'your e-mail is invalid' ); 
        // or display message in form, etc...
        return false;
     } else { 
        return true;
     }    
}

顺便说一句,我不建议使用内联 JS 属性,只需添加到所有元素并形成唯一的 id 属性,如果您使用 jQuery,您可以在.submit()方法中进行所有验证。

于 2012-12-17T09:34:24.080 回答
3

排队:

  <form  method="post" action="{% url sportdub.views.login %}"
   onsubmit="return isValidEmailAddress(this.email.value)">
    <input type="text" name="email" value="" class="span8"/>
    <input type="submit"></button>
  </form>

更好(纯 JS) - 注意我需要一个表单的 ID:

window.onload=function() {
  document.getElementById("form1").onsubmit=function() {
    if (!isValidEmailAddress(this.email.value)) {
      alert('Please enter a valid address');
      return false; // cancel submit
    }
    return true; // allow submit
  }
}

jQuery版本:

$(function() {
  $("#form1").on("submit",function(e) {
    if (!isValidEmailAddress($("#email").val())) {
      alert('Please enter a valid address');
      return false; // cancel submit
    }
    return true; // allow submit
  });
});

最后两个假设

<form  id="form1" method="post" action="{% url sportdub.views.login %}">
  <input type="text" name="email" id="email" value="" class="span8"/>
  <input type="submit"></button>
</form>
于 2012-12-17T09:40:21.903 回答
0

在您的提交按钮中使用一个 onclick 调用并返回...您的代码应该是,

<form  method="post" action="{% url sportdub.views.login %}">
    <input type="text" id="email" name="email" value="" class="span8"/>
    <input type="submit" onclick = "return isValidEmailAddress();"></button>
  </form>

function isValidEmailAddress() {
     var emailAddress = $('#email').val();
     var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
     return pattern.test(emailAddress);
  };
于 2012-12-17T09:36:38.900 回答