This article will introduce to you how bootstrapValidator verifies the maximum and minimum range limits. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
[Related recommendation: "bootstrap tutorial"]
Enter the maximum and minimum values of the value bootstrapValidator for validation, so that The minimum value cannot be greater than the maximum value, and the maximum value cannot be less than the minimum value
The initial verification is still fine, and it conforms to the normal verification rules
Then add the ones that do not comply with the rules The maximum value has changed. Now the minimum value is smaller than the maximum value, but the maximum value has not passed the verification. This means that the verification of the minimum value will only be performed when the content of the minimum value box changes. The content of the maximum value box has not changed and verification will not be performed.
One way is to add this method to the callback to re-validate this field, but both of the two that need to be verified need to be added, resulting in infinite Loop,
//从新验证该字段 $('#form').data('bootstrapValidator').revalidateField('nMax');
You can get the values of two fields by yourself and compare them manually. When the verification rules are met, manually modify the verification status of the field
$('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null );
Compare the two value boxes
$("#form").bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { nMin:{ validators:{ notEmpty:{ message:'最小值不能为空' }, regexp: { regexp: /^[0-9._]+$/, message: '请输入有效的值' }, callback: { message: '最小值不可大于最大值', callback: function(value, validator) { if(value != ''){ var nMax = $("#form").data("bootstrapValidator").getFieldElements('nMax'); if(nMax.val()!='' && Number(nMax.val()) < Number(value)){ return false; }else if(nMax.val()!='' && Number(nMax.val()) > Number(value)){ //从新验证该字段 //$('#form').data('bootstrapValidator').revalidateField('nMax'); //当符合规则手动修改该字段的验证状态 /* 因为上面方法两个字段相互验证 ,会造成死循环,所以在两个字段比较之后,并且符合验证规则,就手动修改该字段的验证状态 */ $('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null ); return true; }else if(nMax.val()==''){ return true; }else{ return true; } }else{ return true; { } } } }, nMax:{ validators:{ notEmpty:{ message:'最大值不能为空' }, regexp: { regexp: /^[0-9._]+$/, message: '请输入有效的值' }, callback: { message: '最大值不可小于最小值', callback: function(value, validator) { if(value != ''){ var nMin = $("#form").data("bootstrapValidator").getFieldElements('nMin'); if(nMin.val()!='' && Number(nMin.val())>Number(value)){ return false; }else if(nMin.val()!='' && Number(nMin.val()) Copy after login
Compare three values
nMin:{ validators:{ notEmpty:{ message:"最小值不能为空" }, regexp: { regexp: /^[0-9._]+$/, message: '请输入有效的值' }, callback: { callback: function(value, validator) { var nSetVal = $("#form").data("bootstrapValidator").getFieldElements('nSetVal'); var nMax = $("#form").data("bootstrapValidator").getFieldElements('nMax'); //最小值大于设定值 if( nSetVal.val()!='' && Number(nSetVal.val()) < Number(value) ){ return { message:'最小值不可大于设定值', flag:false }; //最小值大于最大值 }else if( nMax.val()!='' && Number(nMax.val()) < Number(value) ){ $('#form').data('bootstrapValidator').updateStatus("nSetVal", "VALID", null ); return { message:'最小值不可大于最大值', flag:false }; //最小值 小于 设定值 }else if( nSetVal.val()!='' && Number(nSetVal.val()) >= Number(value) ){ if( Number(nMax.val()) < Number(value) ){ $('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null ); return { message:'最小值不可大于最大值', flag:false }; //最小值 小于设定值 小于最大值 符合全部要求 }else if( nMax.val()!='' && Number(nMax.val()) >= Number(value) && Number(nMax.val()) >= Number(nSetVal.val()) ){ $('#form').data('bootstrapValidator').updateStatus("nSetVal", "VALID", null ); $('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null ); return true; //最小值 小于设定值 大于最大值 }else{ return true; } }else{ return true; } } } } }, nSetVal:{ validators:{ notEmpty:{ message:'设定值不能为空' }, regexp: { regexp: /^[0-9._]+$/, message: '请输入有效的值' }, callback: { callback: function(value, validator) { var nMin = $("#form").data("bootstrapValidator").getFieldElements('nMin'); var nMax = $("#form").data("bootstrapValidator").getFieldElements('nMax'); //设定值小于最小值 if( nMin.val()!='' && Number(nMin.val()) > Number(value) ){ return { message:'设定值不可小于最小值', flag:false }; //设定值 大于 最大值 }else if( nMax.val()!='' && Number(nMax.val()) < Number(value) ){ $('#form').data('bootstrapValidator').updateStatus("nMin", "VALID", null ); return { message:'设定值不可大于最大值', flag:false }; //设定值 小于 最大值 }else if( nMax.val()!='' && Number(nMax.val()) >= Number(value) ){ if( nMin.val()!='' && Number(nMin.val()) > Number(value)){ $('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null ); return { message:'设定值不可小于最小值', flag:false }; //设定值 大于最小值 小于最大值 符合全部要求 }else if( nMin.val()!='' && Number(nMin.val()) <= Number(value) && Number(nMax.val()) >= Number(value) ){ $('#form').data('bootstrapValidator').updateStatus("nMax", "VALID", null ); $('#form').data('bootstrapValidator').updateStatus("nMin", "VALID", null ); return true; }else{ return true; } }else{ return true; } } } } }, nMax:{ validators:{ notEmpty:{ message:"最大值不能为空" }, regexp: { regexp: /^[0-9._]+$/, message: '请输入有效的值' }, callback: { callback: function(value, validator) { var nMin = $("#form").data("bootstrapValidator").getFieldElements('nMin'); var nSetVal = $("#form").data("bootstrapValidator").getFieldElements('nSetVal'); //最小值大于最大值 if( nMin.val()!='' && Number(nMin.val()) > Number(value) ){ return { message:'最大值不可小于最小值', flag:false }; //设定值 大于 最大值 }else if( nSetVal.val()!='' && Number(nSetVal.val()) > Number(value) ){ $('#form').data('bootstrapValidator').updateStatus("nMin", "VALID", null ); return { message:'最大值不可小于设定值', flag:false }; //最小值 小于 最大值 }else if( nMin.val()!='' && Number(nMin.val()) <= Number(value) ){ if(nSetVal.val()!='' && Number(nSetVal.val()) > Number(value)){ $('#form').data('bootstrapValidator').updateStatus("nMin", "VALID", null ); return { message:'最大值不可小于设定值', flag:false }; //最小值 小于最大值 设定值 小于最大值 符合全部要求 }else if( nSetVal.val()!='' && Number(nSetVal.val()) <= Number(value) && Number(nSetVal.val()) >= Number(nMin.val()) ){ $('#form').data('bootstrapValidator').updateStatus("nSetVal", "VALID", null ); $('#form').data('bootstrapValidator').updateStatus("nMin", "VALID", null ); return true; //最小值 小于最大值 设定值 大于最大值 }else{ return true; } }else{ return true; } } } } }
bootstrapValidator Commonly used verification
//不为空验证 notEmpty: { message: '必须输入或者必须选择' } //正整数验证 digits : { message : '字段必须是正整数' } //内容最小值验证 greaterThan: { value : 1, message : '最小输入1' } //内容最大值验证 lessThan: { value : 100 message : '最大输入100' } //正则验证 regexp: { regexp: /^[a-zA-Z0-9_]+$/, message: '正则验证,这里验证只能输入大小写字母数字和下划线' } //内容长度验证 stringLength : { min : 2, max : 25, message : '长度2-25位字符' } //回调验证 callback: { message: '请输入整数', callback: function(value, validator) { //可以return true / false //return false; //也可以这样 return { message:'最小值不可大于最大值', flag:false }; } } //日期验证 date: { format: 'YYYY/MM/DD', message: '日期无效' } //数字验证 digits: { message: '该值只能包含数字。' } //ajax验证 threshold : 6 , //有6字符以上才发送ajax请求,(input中输入一个字符,插件会向服务器发送一次,设置限制,6字符以上才开始) remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值,获得一个json数据。例表示正确:{"valid",true} url: 'exist2.do',//验证地址 message: '用户已存在',//提示消息 delay : 2000,//每输入一个字符,就发ajax请求,服务器压力还是太大,设置2秒发送一次ajax(默认输入一个字符,提交一次,服务器压力太大) type: 'POST'//请求方式 } //复选框验证 choice: { min: 2, max: 4, message: '请选择2-4项' } //密码确认 identical: { field: 'confirmPassword', message: 'The password and its confirm are not the same' }
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of How bootstrapValidator validates maximum and minimum limits. For more information, please follow other related articles on the PHP Chinese website!