简体   繁体   English

如何获取parsley.js来验证字段之一的数字是否具有最小值等于1的数字

[英]how to get parsley.js to validate if one of the fields has number with a with a minimum value equal to 1

I have Adults, Children and Babies fields on my form. 我的表单上有“成人”,“儿童”和“婴儿”字段。 All these 3 fields have a default value which is 0. I am using parsley to validate the form. 所有这3个字段的默认值均为0。我正在使用欧芹来验证表单。 I need parsley to validate if at least one of the fields has a value greater than 0. It's should validate the form if one of the fields is bigger then 0. If not it should give an error when trying to submit. 我需要欧芹来验证至少一个字段的值是否大于0。如果其中一个字段的值大于0,则应该验证表单。如果不是,则应尝试提交。 I used this example from the parsley offical website. 我用从香菜官方网站的例子。

This is what I have so far: 这是我到目前为止的内容:

    <input  type="text" name="nAdults" id="nAdults" value="0" data-parsley-group="block1" class="form-control " />

    <input  type="text" name="nChildren" id="nChildren" value="0" data-parsley-group="block2" class="form-control "/> 

    <input  type="text" name="nBabies" id="nBabies" value="0" data-parsley-group="block3" class="form-control " /> 

<script type="text/javascript">
            //parsley validate
            var form = $('#{{ $pageModule }}FormAjax'); 
            form.parsley().on('form:validate', function (formInstance) {
            var ok = formInstance.isValid({group: 'block1', force: true}) || formInstance.isValid({group: 'block2', force: true}) || formInstance.isValid({group: 'block3', force: true});
            $('.invalid-form-error-message').html(ok ? '' : 'You must fill at least one of the Adult, Child or FOC Fields!').toggleClass('filled', !ok);
            if (!ok)
            formInstance.validationResult = false;
            });
            //form submit
            form.on('submit', function(e) {
                var f = $(this);
                f.parsley().validate();
                if (f.parsley().isValid()) {
                    var options = { 
                        dataType:      'json', 
                        beforeSubmit :  showRequest,
                        success:       showResponse  
                    }  
                    $(this).ajaxSubmit(options); 
                    return false;
                }
                else    { 
                    return false; }
                    e.preventDefault();
            }); 
        </script>

I would appreciate if you can show me how to validate these 3 fields. 如果您能向我展示如何验证这3个字段,我将不胜感激。 When I write 当我写

data-parsley-min="1" it expects all the field to have a minimum value. data-parsley-min="1"它期望所有字段都具有最小值。 But I need only one field to have minimum value "1". 但是我只需要一个字段就可以具有最小值“ 1”。

you have to write custom validator. 您必须编写自定义验证程序。 Here can you find a good example, how to do this. 在这里您可以找到一个很好的例子,该如何做。

my own working example (see console) 我自己的工作示例 (请参阅控制台)

// bind event after form validation
$.listen('parsley:form:validated', function(ParsleyForm) {
// We only do this for specific forms
if (ParsleyForm.$element.attr('id') == 'myForm') {
    var combinations = ParsleyForm.$element.find('input')
    ,counter=0;
    $.each($('input'),function(){
        counter+=($(this).val());
    });
    if(counter>0) 
        ParsleyForm.validationResult = true;
  }
});

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

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