我有一個在模式中打開的窗體。我想爲此實現驗證。但是,我試圖實現jQuery表單驗證,但徒勞無功。無法觸發表單驗證的主要原因是表單提交按鈕是使用JavaScript觸發的。表單提交時不會執行驗證部分。通過javascript觸發提交按鈕時進行表單驗證
這是什麼工作?
這裏是形式::
<div id="__paymentModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Make Payment Request</h3>
</div>
<div class="modal-body">
<form method="post" action="" id="__paymentForm" class="form-horizontal">
<input type="text" name="name" value="" placeholder="Payee Name">
<input type="text" name="phone" value="" placeholder="Phone"/>
<input type="text" name="address" value="" placeholder="Address"/>
<input type="text" name="pincode" value="" placeholder="Pincode" />
<input type="text" name="amount" value="" placeholder="Amount"/>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript: reset()">Close</button>
<button class="btn btn-primary" onclick="javascript: sendPaymentRequest()">Save changes</button>
</div>
</div>
而觸發的形式提交的JavaScript。
var paymentReqBttnObject = undefined;
function reset() {
window.paymentReqBttnObject = undefined;
}
function openModalForPaymentReq(ele) {
$('#__paymentForm input[type="text"]').val("");
$('#__paymentModal').modal({ backdrop: false });
window.paymentReqBttnObject = ele;
}
function sendPaymentRequest() {
$.ajax({
type: "post",
url: url,
data: $('#__paymentForm').serialize(),
processData : true,
dataType: 'html',
beforeSend: function() {
$('#__paymentModal').hide();
},
success: function(data, textStatus, xhr) {
var json = $.parseJSON(data);
if(json.success == "request_made") {
$(paymentReqBttnObject).addClass("btn-requested").html("Payment Requested");
$(paymentReqBttnObject).unbind("click");
} else {
}
window.reset();
},
error: function(xhr, textStatus, errorThrown) {
window.reset();
}
});
}
我也嘗試過jquery表單驗證,但在提交表單時仍然沒有顯示任何錯誤。它馬上轉到僅更新空值的數據庫。
驗證的jQuery
$(document).ready(function(){
$("#__paymentForm").validate({
rules: {
phone: { //input name: fullName
required: true, //required boolean: true/false
minlength: 10,
maxlength: 10
},
address: {
required: true
},
pincode: {
required: true,
minlength: 6,
maxlength: 6
}
amount: {
required: true
}
},
messages: { //messages to appear on error
phone: { //input name: fullName
required: "Your Phone Number", //required boolean: true/false
minlength: "Correct contact number.",
maxlength: "Correct contact number."
},
address: {
required: "Where to collect money."
},
pincode: {
required: "Pincode is required to locate the city and all others.",
minlength: "Correct Pincode Please.",
maxlength: "Correect Pincode Please"
}
amount: {
required: "What is the amount."
},
},
submitHandler: function(form) {
$(form).ajaxSubmit({
success: function(){
alert('inside');
}
});
}
});
})
我看不到您的驗證碼,請問您可以包括它嗎? – Joe
@Joe你走了。忘了補充一點。 –
使用jQuery時,inline'onclick'處理程序是醜陋和不必要的。 – Sparky