是否可以动态禁用某个字段的特定验证器,但仍然希望将其他验证应用于该字段。
示例代码:
$(document).ready(function() {
$('#registrationForm')
.bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
fname: {
validators: {
notEmpty: {
message: 'First name is required and cannot be empty'
},
stringLength: {
min: 6,
max: 30,
message: 'First name must be more than 6 and less than 30 characters long'
},
regexp: {
regexp: /^[a-zA-Z0-9]+$/,
message: 'First name can only consist of alphabetical and digits'
}
}
},
lname: {
validators: {
notEmpty: {
message: 'Last name is required and cannot be empty'
},
stringLength: {
min: 6,
max: 30,
message: 'Last name must be more than 6 and less than 30 characters long'
},
regexp: {
regexp: /^[a-zA-Z0-9]+$/,
message: 'Last name can only consist of alphabetical and digits'
}
}
}
}
})
.on('error.validator.bv', function(e, data) {
data.element
.data('bv.messages')
.find('.help-block[data-bv-for="' + data.field + '"]').hide()
.filter('[data-bv-validator="' + data.validator + '"]').show();
});
});
$(document).on("change keyup paste", "#fName", function () {
if($('#fname').val()==="") { $('#registrationForm').bootstrapValidator('enableFieldValidators', 'lname', true);
} else {
$('#registrationForm').bootstrapValidator('enableFieldValidators', 'lname', false);
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://oss.maxcdn.com/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
</head>
<body>
<form id="registrationForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">First Name</label>
<div class="col-sm-4">
<input type="text" autocomplete="off" class="form-control" name="fname" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Last Name</label>
<div class="col-sm-4">
<input type="text" autocomplete="off" class="form-control" name="lname" />
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-3">
<button type="submit" class="btn btn-default">Validate</button>
</div>
</div>
</form>
</body>
</html>
在此示例中,我要动态禁用对notEmpty
的验证,但仍然希望查看stringLength
的验证消息
我尝试了下面的代码,但是没有任何运气
$('#registrationForm').bootstrapValidator('enableFieldValidators', 'username', 'notEmpty', false);
---更新---
更新了示例以使其变得简单。
我有名字和姓氏字段。如果输入名字,则姓氏不是必需的。这意味着我不需要notEmpty
的验证消息。但是,如果用户决定输入一个姓氏值,那么它应该满足其他stringLength
和regexp
的验证。
答案 0 :(得分:1)
您弄乱了参数命令。根据{{3}},应为:field
,enabled
,validatorName
:
$('#registrationForm').bootstrapValidator('enableFieldValidators',
'username', false, 'notEmpty');
我已经在您的代码段中进行了测试,并且可以正常工作。
答案 1 :(得分:0)
希望您对此有帮助
$(document).ready(function() {
$('#registrationForm')
.bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
username: {
validators: {
notEmpty: {
message: 'The username is required and cannot be empty'
},
stringLength: {
min: 6,
max: 30,
message: 'The username must be more than 6 and less than 30 characters long'
},
regexp: {
regexp: /^[a-zA-Z0-9]+$/,
message: 'The username can only consist of alphabetical and digits'
}
}
}
}
})
.on('error.validator.bv', function(e, data) {
data.element
.data('bv.messages')
.find('.help-block[data-bv-for="' + data.field + '"]').hide()
.filter('[data-bv-validator="' + data.validator + '"]').show();
});
});
$(document).on("change", "#inlineCheckbox1", function () {
if($(this).is(":checked")) {
$('#registrationForm').bootstrapValidator('enableFieldValidators', 'username', true,'notEmpty');
} else {
$('#registrationForm').bootstrapValidator('enableFieldValidators', 'username', false,'notEmpty');
}
});
#registrationForm{
padding:2rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://oss.maxcdn.com/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
</head>
<body>
<form id="registrationForm" method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">Username</label>
<div class="col-sm-4">
<input type="text" autocomplete="off" class="form-control" name="username" />
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-3">
<button type="submit" class="btn btn-default">Validate</button>
</div>
</div>
<div class="form-group">
<label for="inlineCheckbox1">Validate</label>
<input type="checkbox" id="inlineCheckbox1" value="option1" checked>
</div>
</form>
</body>
</html>