验证动态字段引导程序验证器

时间:2018-07-06 05:27:22

标签: javascript bootstrapvalidator

我正在创建销售页面。输入的数量值应小于或等于库存值。

JS - 添加动态行

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);
      };

验证-引导程序验证器

$('#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'
                  }

我如何验证字段数量小于stock_qty

1 个答案:

答案 0 :(得分:1)

使用html5输入类型编号作为数量。然后将最大属性值设置为等于可用库存数量。

   cols += '<td class="paddingtblr5-10"><input type="number" max="'+qty+'" class="form-control input-sm mb3" name="qty[]" id="' + counter + '" placeholder="Enter Quantity" value="1" onchange="findTotal();" onkeyup="findTotal();" required/></td>';