gpt4 book ai didi

javascript - BootstrapValidator - 禁用给定字段的特定验证

转载 作者:行者123 更新时间:2023-11-30 19:54:46 26 4
gpt4 key购买 nike

有没有办法动态禁用字段的特定验证器,但仍希望对同一字段应用其他验证。

示例代码:

   $(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 的验证消息。但是,如果用户决定为姓氏输入一个值,那么它应该满足其他剩余验证 stringLengthregexp

最佳答案

你搞乱了参数顺序。根据source code它应该是:field, enabled, validatorName:

$('#registrationForm').bootstrapValidator('enableFieldValidators',
'username', false, 'notEmpty');

我已经在您的代码段中进行了测试并且有效。

关于javascript - BootstrapValidator - 禁用给定字段的特定验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54118272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com