如何获取 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
我的表格上有成人、儿童和婴儿字段。所有这 3 个字段都有一个默认值,即 0。我使用的是 parsley 官方网站上的 parsley to validate the form. 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. I used this 示例。
这是我目前拥有的:
<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>
如果您能告诉我如何验证这 3 个字段,我将不胜感激。当我写
data-parsley-min="1"
它期望所有字段都具有最小值。但我只需要一个字段具有最小值“1”。
您必须编写自定义验证器。 Here你能找到一个很好的例子吗,如何做到这一点。
my own working example(查看控制台)
// 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;
}
});
我的表格上有成人、儿童和婴儿字段。所有这 3 个字段都有一个默认值,即 0。我使用的是 parsley 官方网站上的 parsley to validate the form. 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. I used this 示例。
这是我目前拥有的:
<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>
如果您能告诉我如何验证这 3 个字段,我将不胜感激。当我写
data-parsley-min="1"
它期望所有字段都具有最小值。但我只需要一个字段具有最小值“1”。
您必须编写自定义验证器。 Here你能找到一个很好的例子吗,如何做到这一点。
my own working example(查看控制台)
// 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;
}
});