I am creating a sales page. The quantity value entered should be less than or equal to stock value.
JS - Adding dynamic rows
function addMed(nm,id, price,qty) {
var newRow = $("<tr>");
var cols = "";
cols += '<td class="paddingtblr5-10"><label class="txt-inverse">'+nm+'</label><input type="hidden" name="med_id[]" value="'+id+'" readonly/><input type="hidden" name="stock_qty[]" id="stock_qty_' + counter + '" value="' + qty + '" readonly/></td>';
cols += '<td class="paddingtblr5-10"><input type="hidden" name="value_price[]" value="'+price+'" readonly/>'+price+'</td>';
cols += '<td class="paddingtblr5-10"><input type="text" class="form-control input-sm mb3" name="qty[]" id="' + counter + '" placeholder="Enter Quantity" value="1" onchange="findTotal();" onkeyup="findTotal();" required/></td>';
cols += '<td class="paddingtblr5-10"><span class="subtot">'+price+'</span></td>';
cols += '<td class="paddingtblr5-10"><input type="button" class="ibtnDel btn btn-md btn-danger btn-xs" value="Delete"></td>';
newRow.append(cols);
$("#medicineTable").append(newRow);
counter++;
$('#finalResult').html("");
findTotal();
// Add new field for Bootstrap Validator
$option = newRow.find('[name="qty[]"]');
$('#addMedForm').bootstrapValidator('addField', $option);
};
Validation - Bootstrap Validator
$('#addMedForm').bootstrapValidator({
container: 'tooltip',
group: 'td',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
'qty[]': {
validators: {
notEmpty: {
message: 'Quantity is required'
},
numeric: {
message: 'The value is not a valid number'
},
lessThan: {
value: 45,
message: 'Please enter a value less than or equal to %s'
}
How do i validate field qty to be less than stock_qty
Use html5 input type number for quantity. And then set max attribute value equal to the available stock quantity.