jQuery验证文本输入字段

时间:2015-06-26 13:11:43

标签: jquery validation

我使用SquareBit候选名单将商品添加到订单表单中。添加后,访问者可以更新数量。

添加到表单的项目输出:

<form name='shortlist_form' id="order" method='POST' >
    <div class="order-item"><h3>Game Birds -&nbsp; Cleaners Disinfectants prod1</h3>
        Qty: <input type="text" name="extra:qty[130]" value="57" />
    </div>
    <div class="order-item"><h3>Pigs -&nbsp; Platinum Programme prod1</h3>
        Qty: <input type="text" name="extra:qty[131]" value="25" />
    </div>   
    <div class="order-item"><h3>Game Birds -&nbsp; Cleaners Disinfectants prod2</h3>
        Qty: <input type="text" name="extra:qty[132]" value="8" />
    </div>
<input type="submit" value="Update" />

我在验证时遇到了问题。我不知道如何阻止额外的:qty [item_id]字段为空。

作为起点我使用此验证码。我需要能够验证每个额外的:数量字段。

$("#order").validate({
    //set this to false if you don't what to set focus on the first invalid input
    focusInvalid: false,
    //by default validation will run on input keyup and focusout
    //set this to false to validate on submit only
    onkeyup: false,
    onfocusout: false,
    //by default the error elements is a <label>
    errorElement: "div",

    //place all errors in a <div id="errors"> element
    errorLabelContainer: $("#errors"),

    rules: {
       "extra:qty[]": {
            required: true,
       }
    },
    messages: {
        "extra:qty[]": {
            required: "Please enter a quantity",
        },
    }               
});

有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

试试这个: -

$('[name*="extra:qty"]').each(function(){
   $(this).rules('add', {
    required: true,        
    messages: {
        required: "Please provide entry"          
    }
   });
});

Demo