【问题标题】:Validate two forms when one's submission depends on the other当一个提交依赖于另一个时验证两个表单
【发布时间】:2013-07-30 22:26:05
【问题描述】:

我正在处理这个项目,但在表单验证方面遇到了困难。我有两个表单,表单 X 和表单 Y,我想使用 jQuery 验证插件来实现客户端验证。这是我第一次使用这个插件,所以如果我的问题看起来很初级,我提前道歉。 我已经设置了表格,以便用户必须填写表格 X 和表格 Y 才能在两个表格中提交数据。一旦完成,Form X 将通过 Ajax 提交,Form Y 作为成功响应的结果提交。提交过程工作正常,所有数据都完美发送。但是,我无法进行验证。我仔细研究了文档和多个论坛,但未能找到可行的解决方案。任何建议或相关文献的链接将不胜感激。以下是相关的 javascript 和 html:

<script>
$("#formYsubmit").click(function(e){
        e.preventDefault();
    $.ajax({
        url: "the resulting page",  
    type: "POST",
        data: formXdata, 
        error: function(resp) {
            console.log(resp);
        },
        success: function(resp) {
            $("#formY").submit();
        }
    });

    $(this).disabled=true;
    $(this).val()="Sending..."
    return false;
});

$('#formX').validate({
    rules: {
        email: {
                required: true,
                email: true
                },
        password: {
                minlength: 6,
                required: true
                },
        full_name: {
                required: true
                },
        site_address: {
                minlength: 5,
                },
        phone: {
                minlength: 10,
                phoneRegex: true
                }
    },
    messages: {
        email: "Please enter a valid email address",
        site_address: "Please enter your website name",
        full_name: "Please enter your name",
        password: {
            required: "Please provide a password",
            minlength: "Your password must be at least 6 characters long"
        },
        phone: "Please enter a valid phone number",         
        email: "Please enter a valid email address"
    },
    highlight: function(label) { 
        $(input).closest('.control-group').addClass('error');
    },
    success: function(label) {
        label.text('OK!').addClass('valid');
    }
});
$('#formY').validate({
    rules: {
        plan_input: {
                required: true,
                },
        fieldA: {
                required: true
                },
        fieldB: {
                required: true
                },
        fieldC: {
                required: true
                },
    },
    highlight: function(label) { 
        $(input).addClass('error');
    },
    success: function(label) {
        label.text('OK!').addClass('valid');
    }
});
</script>


<form id="formX" style="float:left; width:40%;" method="post">
    <div class="control-group">
        <div class="controls">                          
            <input id="full_name" name="full_name" placeholder="Full Name" type="text" />
        </div>
        <label class="error"></label>
    </div>    
    <div class="control-group">
        <div class="controls">
            <input class="required" id="email" maxlength="75" name="email" placeholder="Email ID" type="text" />
        </div>
    </div> 
    <div class="control-group">
        <div class="controls">
            <input class="required" id="password" name="password" placeholder="Password" type="password" />
        </div>
        <label class="error"></label>
    </div> 
    <div class="control-group">
        <div class="controls">
            <input id="site_address" name="site_address" placeholder="Website" type="text" />
        </div>
        <label class="error"></label>
    </div>
    <div class="control-group">
        <div class="controls">
            <input id="phone" maxlength="25" name="phone" placeholder="Phone" type="text" />
        </div>
        <label class="error"></label>
    </div>
</form>
<div id="frontServerErrorContainer"></div>
<form id="formY" style="float:left;width:40%;" method="post" action="">
    <select name="plan_input" class="field" id="plan-select" disabled>
        <option value="">Choose Plan</option>
        <option value="1">Option A</option>
        <option value="2">Option B</option>
        <option value="3">Option C</option>
    </select>
    <div id="serverErrorContainer2"></div>
    <div id="errorContainer2"><ul id="errorContainer2"></ul></div>
    <div class="sh form-field cl-div control-group" >
        <input type='text' class="field" name='fieldA' value="" placeholder="fieldA" id="fieldA">
    </div>
    <div class="sh form-field cl-div control-group" >
        <input type='text' class="field" name='fieldB' value="" placeholder="fieldB" id="fieldB">
    </div>
    <div class="sh form-field cl-div control-group" >
        <input type='text' class="field" name='fieldC' value="" placeholder="fieldC" id="fieldC">
    </div>
    <button class="bootstrapbtn bootstrapbtn-primary" id="formYsubmit">Submit</button>
</form>

对于非常长的 sn-p 代码或者这个问题过于简单,我深表歉意。谢谢!

【问题讨论】:

  • 专注于文档中的submitHandler回调函数。
  • 谢谢。我现在正在研究一个使用它的示例案例。将在几个小时内发布结果
  • 我得回去工作了,但这是我到现在为止的。我可能缺少一些明显的 JS 东西jsfiddle.net/DrKFM
  • 看我的回答。另外,去掉submitHandler 中的e.preventDefault()。可以保留return false

标签: javascript ajax jquery-validate


【解决方案1】:

您的 ajax 属于 Validate 插件的 submitHandler 回调。您还需要将所有内容包装在 DOM 就绪处理程序函数中,以确保在调用 .validate() 时 HTML 存在。在.validate() 之外,您不需要任何ajax,也不需要任何额外的click 处理程序。

<script>

    $(document).ready(function() {

        $('#formX').validate({
            // rules & options here
        });

        $('#formY').validate({
            // rules & options here,
            submitHandler: function(form) {
                // only fires when form is valid
                $("#formYsubmit").disabled=true;
                $("#formYsubmit").val()="Sending..."
                form.serialize();
                // your ajax here
                return false; // prevent the regular form submit when using ajax
            }
        });

    });

</script>

文档http://jqueryvalidation.org/validate/

submitHandler(默认:原生表单提交)
类型:Function()
回调 用于在表单有效时处理实际提交。获取表单 作为唯一的论据。替换默认提交。 正确的地方 验证后通过 Ajax 提交表单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2014-07-11
    相关资源
    最近更新 更多