如何在Jquery验证中的一个错误中验证两个字段

时间:2017-02-07 12:36:54

标签: javascript jquery jquery-validate

我有两个date_start和date_end字段,我想在jQuery Validate中验证它们。

我的验证器有两个错误,但我只需要一个......

enter image description here

java JSP表单的一部分

<div class="form-group user-edit-form-group">
  <label for="input_cameras_limit" class="col-xs-4 control-label">Cameras limit</label>

  <div class="col-xs-4">
    <input name="key_cameras_limit" id="input_cameras_limit" type="text"
    class="form-control user-edit-input" placeholder="" value="">
  </div>

  <div class="user-edit-error-wrapper">
  </div>
</div>

JS验证部分

$('#partner_key_settings_form').validate({

    rules: {
        key_valid_from: {
            required: true,
            date: true
        },
        key_valid_till: {
            required: true,
            date: true
        }

    },
    messages: {
        key_valid_from: {
            required: JsMultiLang.getWords('partner', 'validationPromoCodeDate')

        },
        key_valid_till: {
            required: JsMultiLang.getWords('partner', 'validationPromoCodeDate')
        }
    },

    errorClass: "user-search-error",

    errorPlacement: function (error, element) {

        if (element.attr("name")=="key_valid_from" ||element.attr("name")=="key_valid_till"){
             error.appendTo($(element).parent().parent().parent().find('.user-edit-error-wrapper'));
             console.log(error.appendTo($(element).parent().parent().parent().find('.user-edit-error-wrapper')==undefined))
        }

    },

    highlight: function (element) {
        $(".user-edit-server-error").remove();
        $(element).closest('.form-group').addClass('has-error');

        var tabPaneId = $(element).closest('.tab-pane').attr('id');
        $('a[href="#' + tabPaneId + '"]').addClass('has-error');
    },

    unhighlight: function (element, error, valid) {
        $(element).closest('.form-group').removeClass('has-error');

        var tabPaneId = $(element).closest('.tab-pane').attr('id');
        var hasErrorDivsId = '#' + tabPaneId + ' .has-error';
        var errorDivsOnTabSize = $(hasErrorDivsId).size();
        if (errorDivsOnTabSize < 1) {
            $('a[href="#' + tabPaneId + '"]').removeClass('has-error');
        }
    },

    success: function (error) {
        error.remove();

    }
})

1 个答案:

答案 0 :(得分:0)

您需要添加```{r initialize, echo=F, comment=NA} htmPrint = function(htm) { structure(htm, class='knit_asis') } doAnalysis = function(dat, depVar, indVar) { print(htmPrint(paste0('<h3>AusMCP1 on ', indVar, '</h3>'))) eval(parse(text=paste0('print(summary(lm(', depVar, '~', indVar, ', data=dat)))'))) print(htmPrint('<hr>')) } demoData = data.frame(dep1=rnorm(100), dep2=rnorm(100), ind1=runif(100), ind2=runif(100), ind3=runif(100)) varDep = names(demoData)[1:2] varInd = names(demoData)[3:5] ``` This is a small sample of many separate analyses I want to run together, flagging each analysis with a heading generated in the function. I could use *results=asis* in the chunk command, but it produces unwanted formatting and extraneous output in the analysis output. ```{r doAnalyses} htmPrint('<h2>Begin analyses</h2>') for (k in 1:length(varDep)) for (i in 1:length(varInd)) doAnalysis(demoData, varDep[k], varInd[i]) ``` group以便对验证进行分组,并根据需要添加object

errorPlacement