简体   繁体   English

bootstrapValidator-enable和disable字段验证

[英]bootstrapValidator-enable and disable field validation

So; 所以; I tried to check couple of solutions online but I couldn't find a solution that solve my problem regarding enabling and disabling field validation using bootstrapvalidator. 我尝试在线检查几个解决方案,但我找不到解决方案来解决我使用bootstrapvalidator启用和禁用字段验证的问题。

I have a field that supposed to give error when someone enter a negative number (eg -4,-100,-545 etc.), and any number that is not numeric (eg 4.4.4, 4.r, 4t etc). 当有人输入负数(例如-4,-100,-545等)和任何非数字的数字(例如4.4.4,4.r,4t等)时,我有一个字段应该给出错误。 But I want to put a condition that wont give error when a user type in -999. 但是我想在用户输入-999时设置一个不会出错的条件。

Code is working well when a user enter other number that is not a negative and nob-numeric. 当用户输入非负数和数字数字的其他数字时,代码运行良好。 And if a user enters negative or non-numeric number, error is displayed. 如果用户输入负数或非数字数字,则会显示错误。 The problem when user enters -999 it validate true(don't show error), but if user change number(-999) to other number such as negative number and nun-numeric number, my field validation stop working (meaning don't show any error though it was supposed to show). 当用户输入-999时验证为true(不显示错误),但如果用户将数字(-999)更改为其他数字(如负数和非数字数),我的字段验证将停止工作(意味着不要显示任何错误虽然它应该显示)。

So how can I enable and disable field validation or setting up a condition on field validation to solve my problem using bootstrapValidator..??? 那么如何启用和禁用字段验证或设置字段验证条件以使用bootstrapValidator解决我的问题.. ???

Hope you guys have already come across with such a problem and I hope you can help me solve the puzzle. 希望你们已经遇到过这样的问题,我希望你能帮我解决这个难题。

You can try it here: https://jsfiddle.net/os3b0dqx/ to see how it works 你可以在这里试试: https//jsfiddle.net/os3b0dqx/看看它是如何工作的

my template.html looks like:
<form class="well form-horizontal" action=" " method="post"  id="myform">
<div class="form-group">
<label class="col-xs-3 control-label">Length</label>
<div class="col-xs-5">
<input type="text" class="form-control" id="length" name="length" />
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-1">
<button type="submit" class="btn btn-default">Submit</button>
</div> 
</div>
</form>
<script>
$(document).ready(function () {
$('#myform').bootstrapValidator({
feedbackIcons: {
validating: 'glyphicon glyphicon-refresh'    
 },
fields: { 
length: {
validators: {
greaterThan: {
value:0,
message: 'The value must be greater than or equal to zero'
 },
numeric: {
message: 'The value is not a number',
thousandsSeparator: '',
decimalSeparator: '.'
},
notEmpty: {
message: 'Please fill the length' }
}
},
}
}).on('click keyup input change','[name="length"]',function(){
if ($(this).val() == -999) {
$('#myform').bootstrapValidator('enableFieldValidators','length',false);
  }
else{
$('#myform').bootstrapValidator('validateField','length');
//$('#myform').bootstrapValidator('enableFieldValidators','length',true);
// both "validateField" and "enableFieldValidators" doesn't work for me..
}
});
});

将此if ($(this).val() == -999)更改为if ($(this).val() == '-999')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM