如何通过Parsley.js动态添加和删除maxlength验证? [英] How to dynamically add and remove maxlength validation by Parsley.js?
问题描述
我正在使用 parsley.js ,我必须根据用户拥有的数据动态显示/隐藏验证我已经读过这个 SO post 以动态添加或删除要被欧芹验证的必填字段,这是有帮助的。
但是如何动态删除并添加输入字段的 maxlength ?
我尝试了以下两个选项:
$('input')。attr('data-parsley -maxlength','true');
$('input')。attr('data-parsley-maxlength',true);
但是上面提供了以下错误消息:
此值太长。它应该有真正的字符或更少。
我估计只需要分配正确的值类型属性 data-parsley-maxlength
这是数值,见这里: http://parsleyjs.org/doc/#validators-overview
Maxlength#2.0 - > data-parsley-maxlength =10 - >验证字符串的长度
不大于给定的限制。
希望这将适合您的需要:)
尝试这样:
// destroy parsley
$('form')。parsley()。
//将输入的必需属性设置为值类型
$('input')。attr('data-parsley-maxlength','0');
// reitialize parsley
$('form')。parsley();
I am using parsley.js and I have to dynamically show/hide validation according to the data the user has selected.
I have read this SO post to dynamically add or remove required fields to be validated by parsley, which was helpful.
But how do I dynamically remove and add the maxlength for an input field?
I have tried the following two options:
$('input').attr('data-parsley-maxlength', 'true');
$('input').attr('data-parsley-maxlength', true);
But the above gives the following error message:
This value is too long. It should have true characters or fewer.
I reckon you just need to assign correct value type to the attribute, data-parsley-maxlength
which is numerical value, see here: http://parsleyjs.org/doc/#validators-overview
Maxlength #2.0 --> data-parsley-maxlength="10" --> Validates that the length of a string is not larger than the given limit.
Hope this will fit your need :)
Try this:
//destroy parsley
$('form').parsley().destroy();
//set required attribute on input to value type
$('input').attr('data-parsley-maxlength', '0');
//reinitialize parsley
$('form').parsley();
这篇关于如何通过Parsley.js动态添加和删除maxlength验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!