简体   繁体   English

如果验证失败,防止jQuery表单提交

[英]prevent jQuery form submit if validation fail

Hi I'm trying to prevent this AJAX submit form function when a validation fails on one of one of the various inputs. 嗨,我正在尝试通过各种输入之一验证失败时阻止此AJAX提交表单功能。

Edit: What I want to know exactly is what I need to include in //Adult age validation and var handleFormSubmit to stop saveForm($('#quotecriteria_form')); 编辑:我想确切地知道我需要包含在//成人年龄验证和var handleFormSubmit中,以停止saveForm($('#quotecriteria_form')); and performAjaxSubmit(formData, url); 和performAjaxSubmit(formData,url); from occurring. 从发生。

Submit function: 提交功能:

  var handleFormSubmit = function( e ) {
e.preventDefault();

var $submit = $('button[type="submit"]');
$submit.addClass('loading');

// Save the contents of the form to session cookie
saveForm($('#quotecriteria_form'));

var formData = $('#quotecriteria_form').serialize();
var url = $('#quotecriteria_form').attr('action');
performAjaxSubmit(formData, url);
}

validation functions initialized: 验证功能已初始化:

IndexPageFunctions = {
init: function() {
//Validate date range
this.validateToDate();
this.validateFromDate();
//Validate adult age
this.validateAdultAge0();
this.validateAdultAge1();
this.validateAdultAge2();
this.validateAdultAge3();
//Validate child age
this.validateChildAge0();
this.validateChildAge1();
this.validateChildAge2();
this.validateChildAge3();
this.validateChildAge4();
this.validateChildAge5();
this.validateChildAge6();
this.validateChildAge7();
this.validateChildAge8();
},

One of the many validation functions: 众多验证功能之一:

//Adult age validation
validateAdultAge0: function() {
    $('button[type="submit"]').on('click', function() {
var inputVal = parseInt( $("input[name='adultAges[0]']").val());
if (inputVal < 18) {
    $("input[name='adultAges[0]']").css("border-color","red");
$("div#under18").addClass('show').removeClass('hidden');
}
        });
},

Define a boolean array outside the scope of all functions with an index for each input element: 在所有函数范围之外定义一个布尔数组,并为每个输入元素创建一个索引:

var valid = [false,false,false...];

Set the original value to false if blank input should be considered invalid or true if the field is optional. 如果应将空白输入视为无效,则将原始值设置为false;如果该字段为可选,则将其设置为true。 In each of your validation handlers (like validateAdultAge0) change the corresponding flag accordingly. 在每个验证处理程序(例如validateAdultAge0)中,相应地更改相应的标志。 For example if you decide to map the validity of age0 to the 0th index then: 例如,如果您决定将age0的有效性映射到第0个索引,则:

if (inputVal < 18) {
    $("input[name='adultAges[0]']").css("border-color","red");
    $("div#under18").addClass('show').removeClass('hidden');
    valid[0] = false;
}else{
    valid[0] = true;
}

Like someone in the comments pointed out, if the logic is the same for each of these functions you should be using an argument to specify the target input, not creating an entirely new function but I won't get into that here. 就像评论中指出的人一样,如果每个函数的逻辑相同,则应该使用一个参数指定目标输入,而不是创建一个全新的函数,但我在这里不做介绍。

In handleFormSubmit you'll just need to check if all the flags in the valid array are set to true, if any one is false you can return: 在handleFormSubmit中,您只需要检查有效数组中的所有标志是否都设置为true,如果任何一个为false,则可以返回:

for(var i=0;i<valid.length;i++){
    if(!valid[i]){
        return;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM