Home Web Front-end Bootstrap Tutorial How bootstrapValidator validates maximum and minimum limits

How bootstrapValidator validates maximum and minimum limits

May 17, 2021 am 10:24 AM
bootstrap

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.

How bootstrapValidator validates maximum and minimum limits

[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

How bootstrapValidator validates maximum and minimum limits
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.

How bootstrapValidator validates maximum and minimum limits

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');
Copy after login

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 );
Copy after login

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');
									 
									//当符合规则手动修改该字段的验证状态
									/* 因为上面方法两个字段相互验证 ,会造成死循环,所以在两个字段比较之后,并且符合验证规则,就手动修改该字段的验证状态 */
									$(&#39;#form&#39;).data(&#39;bootstrapValidator&#39;).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) ){
									 
									$(&#39;#form&#39;).data(&#39;bootstrapValidator&#39;).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 );
									$(&#39;#form&#39;).data(&#39;bootstrapValidator&#39;).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)){
									 
									$(&#39;#form&#39;).data(&#39;bootstrapValidator&#39;).updateStatus("nMax", "VALID", null );	
									return { message:'设定值不可小于最小值', flag:false };
									 
								//设定值 大于最小值 小于最大值   符合全部要求
								}else if( nMin.val()!='' && Number(nMin.val()) <= Number(value) && Number(nMax.val()) >= Number(value) ){
									 
									$(&#39;#form&#39;).data(&#39;bootstrapValidator&#39;).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;
							}
						}
					}
				}
			}
Copy after login

bootstrapValidator Commonly used verification

//不为空验证
notEmpty: {
     message: &#39;必须输入或者必须选择&#39;
}

//正整数验证
digits : {
    message : &#39;字段必须是正整数&#39;
}

//内容最小值验证
greaterThan: {
    value : 1,
    message : &#39;最小输入1&#39;
}

//内容最大值验证
lessThan: {
    value : 100
    message : &#39;最大输入100&#39;
}

//正则验证
regexp: {
    regexp: /^[a-zA-Z0-9_]+$/,
    message: &#39;正则验证,这里验证只能输入大小写字母数字和下划线&#39;
}

//内容长度验证
stringLength : {
    min : 2,
    max : 25,
    message : &#39;长度2-25位字符&#39;
}

//回调验证
callback: {
    message: &#39;请输入整数&#39;,
	callback: function(value, validator) {	
			//可以return   true  /  false
			//return  false;
			//也可以这样
			return { message:&#39;最小值不可大于最大值&#39;, flag:false };
	}
}

//日期验证
date: {
  	format: &#39;YYYY/MM/DD&#39;,
    message: &#39;日期无效&#39;
}

//数字验证
digits: {
    message: &#39;该值只能包含数字。&#39;
}

//ajax验证
threshold :  6 , //有6字符以上才发送ajax请求,(input中输入一个字符,插件会向服务器发送一次,设置限制,6字符以上才开始)
remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值,获得一个json数据。例表示正确:{"valid",true}  
    url: &#39;exist2.do&#39;,//验证地址
    message: &#39;用户已存在&#39;,//提示消息
    delay :  2000,//每输入一个字符,就发ajax请求,服务器压力还是太大,设置2秒发送一次ajax(默认输入一个字符,提交一次,服务器压力太大)
    type: &#39;POST&#39;//请求方式
 }
 
//复选框验证
choice: {
  	min: 2,
    max: 4,
    message: &#39;请选择2-4项&#39;
}

//密码确认
identical: {
	field: &#39;confirmPassword&#39;,
    message: &#39;The password and its confirm are not the same&#39;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

See all articles