preventDefault()停止表单验证

ale*_*ela 6 javascript forms validation jquery

使用.preventDefault()在浏览器表单验证方面遇到问题

有没有办法让浏览器检查所需输入的验证,但是停止提交?

如果表格有效,我可以使用任何标志吗?

谢谢

更新:同时使用backbone和jquery

events: {
    "click #commentFormSubmit": "commentFormSubmit",
},
commentFormSubmit: function(el){
    el.preventDefault();
    var $el = $(el.target).parent();

    // this.$el.find('button').prop('disabled', true).addClass('disabled');
    var commentData = {};
    commentData.name = this.$el.find('input[name=comment_name]').val();
    commentData.country = this.$el.find('input[name=comment_country]').val();
    commentData.email = this.$el.find('input[name=comment_email]').val();
    commentData.comment = this.$el.find('textarea[name=comment_text]').val();
    commentData.grade = this.$el.find('.commnt_grade:checked').val();
    console.log('dd')
    this.model.onSubmitComment(commentData);
},
Run Code Online (Sandbox Code Playgroud)

和形式:

    <form action="" class="" method="post">
        <span>
            <input type="text" name="comment_name" class="comment_input" placeholder="{{ 'your name'|_ }}" required>
            <input type="text" name="comment_country" class="comment_input" placeholder="{{ 'country'|_ }}">
            <input type="text" name="comment_email" class="comment_input" placeholder="{{ 'your email'|_ }}" required>
        </span>

        <textarea name="comment_text" id="comment_text" cols="30" rows="10" placeholder="{{ 'your comment'|_ }}" required></textarea>
        <span class="grades">

            <input type="radio" name="commnt_grade" class="commnt_grade" id="grade_1" value="1">
            <label for="grade_1" class="selectGrades" data-eq="1"></label>

            <input type="radio" name="commnt_grade" class="commnt_grade" id="grade_2" value="2">
            <label for="grade_2" class="selectGrades" data-eq="2"></label>

            <input type="radio" name="commnt_grade" class="commnt_grade" id="grade_3" value="3">
            <label for="grade_3" class="selectGrades" data-eq="3"></label>

            <input type="radio" name="commnt_grade" class="commnt_grade" id="grade_4" value="4">
            <label for="grade_4" class="selectGrades" data-eq="4"></label>

            <input type="radio" name="commnt_grade" class="commnt_grade" id="grade_5" value="5">
            <label for="grade_5" class="selectGrades" data-eq="5"></label>
        </span>
        <button type="submit" id="commentFormSubmit">{{ 'submit'|_ }}</button>

    </form>
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 6

如果您想在不提交的情况下进行验证,则可以让浏览器通过调用来检查表单的有效性checkValidity,并通过来报告有效性reportValidity。(在支持HTML验证的浏览器上。)

既调用检查又调用报告,而无需提交:

yourFormElement.checkValidity();
yourFormElement.reportValidity();
Run Code Online (Sandbox Code Playgroud)

例:

yourFormElement.checkValidity();
yourFormElement.reportValidity();
Run Code Online (Sandbox Code Playgroud)
$("input[type=button]").on("click", function() {
  var yourFormElement = $("form")[0];
  yourFormElement.checkValidity();
  yourFormElement.reportValidity();
});
Run Code Online (Sandbox Code Playgroud)

您也可以在单个元素级别执行此操作:

yourInputElement.checkValidity();
yourInputElement.reportValidity();
Run Code Online (Sandbox Code Playgroud)

例:

<form>
<input type="text" required>
<br><input type="text" required>
<br><input type="button" value="Click to check">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
yourInputElement.checkValidity();
yourInputElement.reportValidity();
Run Code Online (Sandbox Code Playgroud)

既然已经提到您正在使用jQuery,那么我只强调这些是DOM元素的方法,而不是jQuery对象。因此,例如,如果您的表单有一个,id并且您看起来像这样:

var $myForm = $("#the-form");
Run Code Online (Sandbox Code Playgroud)

那你就用

$myForm[0].checkValidity();
$myForm[0].reportValidity();
Run Code Online (Sandbox Code Playgroud)

$myForm.checkValidity();  // Wrong
$myForm.reportValidity(); // Wrong
Run Code Online (Sandbox Code Playgroud)

或者您可以给自己一个小插件:

jQuery.fn.checkValidity = function() {
    var el = this[0];
    return el && el.checkValidity();
};
jQuery.fn.reportValidity = function() {
    var el = this[0];
    return el && el.reportValidity();
};
Run Code Online (Sandbox Code Playgroud)

这与jQuery的其他“ getter”保持一致,因为它仅查看jQuery对象内部集合中的第一个元素。


hol*_*erd 5

虽然@TJ 在他的回答中提供了一个很好的解决方案,但另一种方法是submit在使用event.preventDefault(). 这将允许触发表单验证但阻止表单提交。


Vad*_*imB -3

您始终可以依靠 HTML5 表单验证属性来阻止您在表单数据无效时提交请求:

  • 残疾人
  • 最大限度
  • 分钟
  • 图案
  • 必需的

因此,您不需要阻止默认行为 - 使用无法提交表单,直到您有无效数据。

<form action="/action_page_post.php" method="post">
  <input type="text" name="fname" required>
  <input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

https://www.w3schools.com/js/js_validation.asp