我正在構建付款表單並使用jQuery驗證來執行我的驗證。只有當它有2個小數位時,我如何驗證「付款金額」字段?如果用戶在金額上減去.00,我希望它失敗。在jQuery驗證的文本輸入中需要2位小數
這裏是我的javascript:
$(document).ready(function() {
$('#payment-form').validate({ // initialize the plugin
rules: {
order_id: {
required: true
},
price: {
required: true,
number: true
}
},
messages: {
order_id: "Please enter your Invoice Number",
price: "Please enter Payment Amount"
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
//form.submit();
}
});
$('#price').keypress(function(event) {
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
if( ($(this).val().indexOf('.') != -1) && ($(this).val().substring($(this).val().indexOf('.'),$(this).val().indexOf('.').length).length>2) ){
event.preventDefault();
}
});
});
而我的HTML:
<form id="payment-form" class="user-forms" method="post">
<p class="invoice">
<label for="order_id">Invoice Number<font color="red" title="This field is marked as required by the administrator.">*</font></label>
<input class="text-input" name="order_id" type="text" id="order_id" value="">
</p>
<p class="payment">
<label for="price">Payment Amount<font color="red" title="This field is marked as required by the administrator.">*</font></label>
<input class="text-input" name="price" type="text" id="price" value="">
<span class="wppb-description-delimiter">You must enter an amount to 2 decimal places (i.e. 19.00)</span>
</p>
<hr/>
<p class="form-submit">
<input type="hidden" name="cust_id" value="<?php echo $current_user->user_firstname . ' ' . $current_user->user_lastname; ?>">
<input type="hidden" name="email" value="<?php echo $current_user->user_email; ?>">
<input name="finalize_payment" type="submit" id="finalize_payment" class="submit button btn btn-blue" value="Finalize Payment">
</p>
</form>
提前感謝!
可能重複[如何驗證在jQuery的錢](http://stackoverflow.com/questions/21017867/how- jQuery的 – Barmar
@Barmar,這個問題沒有解釋如何將它整合到使用jQuery Validate的解決方案中。 – Sparky
第二個答案顯示瞭如何創建一個新的驗證方法'money'。 – Barmar