我有一个模式弹出窗口。其中有两个字段,“已接收数量”和“总数量”。 如果有人输入“收到的数量”,则从jquery中使用“总量”进行验证。
总数量来自数据库。并且它是隐藏的。
$("#update-receivings").click(function() {
var received_quantity1 = $.trim($('#received_quantity').val());
var total_quantity1 = $.trim($('#total_quantity').val());
var intRegex = /^[1-9]\d*$/;
if (received_quantity1 > total_quantity1) {
alert('bad>');
$('.error1').show();
$('.error2').hide();
$('#received_quantity1').val('');
} else if (received_quantity1 < '1') {
alert('bad<');
$('.error1').show();
$('.error2').hide();
$('#received_quantity1').val('');
} else if (!received_quantity1.match(intRegex)) {
alert('badr');
$('.error2').show();
$('.error1').hide();
$('#received_quantity1').val('');
} else {
alert('good');
//$('#drug-receive-form').submit();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="form-horizontal" method="POST" id="drug-receive-form">
<div class="modal-body">
<!--<div class="form-group">
<div class="col-sm-12">
<input type="password" name="supply_code" id="supply_code" class="form-control">
<span class="text-red" id="otp-error"></span>
</div>
</div>-->
<input type="hidden" name="total_quantity" id="total_quantity"> Received Quantity: <input type="text" name="received_quantity" id="received_quantity"><br>
<span class="error1" style="color: Red; display: none">* Received Quantity Should not be greater than Available Quantity and should not less than 1</span>
<span class="error2" style="color: Red; display: none">* Loose Quantity is not acceptable.</span>
</div>
<div class="modal-footer">
<div class="text-left">
<!--<button type="button" name="update-receivings" id="update-receivings" class="btn btn-info">Submit</button>-->
<button type="button" name="update-receivings" id="update-receivings" class="btn btn-info">Yes </button>
<!--<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>--->
<button type="button" class="btn btn-danger" data-dismiss="modal">Not Now </button>
</div>
</div>
</form>
它不起作用。如果总数量为45 ,并且我输入已接收数量5.5 ,则它将处于第一状态。匹配5.5大于45。
答案 0 :(得分:1)
使用parseInt javascript函数比较数字。
var received_quantity1 = parseInt($.trim($('#received_quantity').val()));
var total_quantity1 = parseInt($.trim($('#total_quantity').val()));
答案 1 :(得分:0)
因为首先您需要检查正则表达式是否匹配而不是检查其他条件,所以请更改条件的顺序。
<script>
$( "#update-receivings" ).click(function() {
var received_quantity1 = $.trim($('#received_quantity').val());
var total_quantity1 = $.trim($('#total_quantity').val());
var intRegex = /^[1-9]\d*$/;
if(!received_quantity1.match(intRegex)){
alert('badr');
$('.error2').show();
$('.error1').hide();
$('#received_quantity1').val('');
}
else{
alert('good');
if( received_quantity1 > total_quantity1 )
{
alert('bad>');
$('.error1').show();
$('.error2').hide();
$('#received_quantity1').val('');
}
else if( received_quantity1 < '1' )
{
alert('bad<');
$('.error1').show();
$('.error2').hide();
$('#received_quantity1').val('');
}
else{
$('#drug-receive-form').submit();
}
}
});
</script>
答案 2 :(得分:0)
我以这种方式找到了答案:
<script>
$( "#update-receivings" ).click(function() {
var received_quantity1 = parseInt($.trim($('#received_quantity').val()));
var total_quantity1 = parseInt($.trim($('#total_quantity').val()));
var received_quantity2 = $.trim($('#received_quantity').val());
if(received_quantity1 != received_quantity2){
//alert('badr');
$('.error2').show();
$('.error1').hide();
$('#received_quantity2').val('');
}
else{
if( received_quantity1 > total_quantity1 )
{
//alert('bad>');
$('.error1').show();
$('.error2').hide();
$('#received_quantity2').val('');
}
else if( received_quantity1 < '1' )
{
//alert('bad<');
$('.error1').show();
$('.error2').hide();
$('#received_quantity2').val('');
}
else{
//alert('good');
$('#drug-receive-form').submit();
}
}
});
</script>