Bootstrap Validator最小长度验证失效

菜鸡的逆袭之路 2017-01-18 09:53:25
RT,今天在使用Bootstrap Validator时,发现调用ajax验证时设置了threshold,但是输入第一个字符的时候已经开始执行ajax数据交互了(其他设置都没问题),导致每输入一个字符就会调用一次,给服务器造成了极大的压力,想请问一下老师是不是什么位置写错了,怎么修改。

fields: {/*验证:规则*/
'account.accountNo': {//验证input项:验证规则
message: '用户名非法',
validators: {
notEmpty: {//非空验证:提示消息
message: '用户名不能为空'
},
stringLength: {
min: 8,
max: 16,
message: '用户名长度必须在8到16之间'
},
threshold: 8,
//有8字符以上才发送ajax请求,(input中输入一个字符,插件会向服务器发送一次,设置限制,6字符以上才开始)
remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值,获得一个json数据。例表示正确:{"valid",true}
url: 'exist.action',//验证地址
message: '用户已存在',//提示消息
delay : 2000,//每输入一个字符,就发ajax请求,服务器压力还是太大,设置2秒发送一次ajax(默认输入一个字符,提交一次,服务器压力太大)
type: 'POST'//请求方式
/**自定义提交数据,默认值提交当前input value
* data: function(validator) {
return {
password: $('[name="passwordNameAttributeInYourForm"]').val(),
whatever: $('[name="whateverNameAttributeInYourForm"]').val()
};
}
*/
},
regexp: {
regexp: /^[a-zA-Z]{1}[a-zA-Z0-9]{7,15}$/,
message: '用户名由8-16位数字字母组成,且第一个字符必须为字母'
}
}
},
... ...

Bootstrap Validator相关代码:

... ...
/**
* Check if the number of characters of field value exceed the threshold or not
*
* @param {jQuery} $field The field element
* @returns {Boolean}
*/
_exceedThreshold: function($field) {
var field = $field.attr('data-bv-field'),
threshold = this.options.fields[field].threshold || this.options.threshold;
if (!threshold) {
return true;
}
var cannotType = $.inArray($field.attr('type'), ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit']) !== -1;
return (cannotType || $field.val().length >= threshold);
},
... ...
...全文
594 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao鬼 2017-05-28
  • 打赏
  • 举报
回复
threshold = this.options.fields[field].threshold || this.options.threshold; 源码写的很清楚了,可以设置在基本配置统一所有字段的最小验证长度,也可以单独为每个字段设置最小的验证长度,也就是.fields[field].threshold,所以你不应该把threshold 写在validators里面,往上提一级,与message和validators同一级就可以了。如下: fields:{ message: '用户名非法', threshold :8, validators: {........} } 这样子就行了。又或者 threshold :8, fields:{........} 这样也行,这个就是同一配置所有字段的最小验证长度。
forwardNow 2017-01-19
  • 打赏
  • 举报
回复
https://github.com/nghuuphuoc/bootstrapvalidator This repository is for anyone who still use the BootstrapValidator. It's no longer supported. Please upgrade to use FormValidation. 已经停止更新了啊,API页都找不着。

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧