2010-11-16 62 views
3

我在這裏撕掉我的頭髮試圖獲得一個jQuery驗證與我的表單很好地發揮。jquery驗證不會停止提交形式

驗證似乎沒有工作。表單只是提交給頁面。我可以簡要地看到出現的驗證錯誤消息的頁面提交之前...

我的代碼:

//HTML form 
<form id="form_scheduleEvent" name="form_scheduleEvent"> 
    <label for="name">Name:</label><input class="short" type="text" name="name" id="name" /> 
    <label for="address">Address:</label><input type="text" name="address" id="address" /> 
    <label for="phone">Phone:</label><input type="text" name="phone" id="phone" /> 
    <label for="comments">Comments:</label><textarea name="comments" id="comments" /></textarea> 
    <input type="submit" id="submitRequest" value="Add"/> 
</form> 


//jquery 
//Validation rules 
$('#form_scheduleEvent').validate({ 
rules: { 
    name : {required: true, maxlength: 45}, 
    address : {required: true, maxlength: 45}, 
    phone : "required" 
} 
}); 


$('#submitRequest').click(function(){ 
    $.ajax({ 
     type: "POST", 
     url: "common/ajax_event.php", 
     data: formSerialized, 
     timeout:3000, 
     error:function(){alert('Error');}, 
     success: function() {alert('It worked!');} 
    }); 

return false; 
}); 

我試圖更新到這兩個jQuery和jquery.validation的最新版本....

任何幫助,將不勝感激!謝謝。

更新

//The validation is working, but I can't even get the alert to show.... 
$('#form_scheduleEvent').validate({ 
rules: { 
    name : {required: true, maxlength: 45}, 
    address : {required: true, maxlength: 45}, 
    phone : "required", 
    submitHandler: function(){ 
    alert('test'); 
    } 
} 
}); 
+1

請發佈ajax_event.php – Catfish 2010-11-16 16:39:37

+1

看起來你完全繞過了插件。你正在做一個獨立於驗證的ajax請求。 (點擊發送表單,而不是提交) – 2010-11-16 16:39:50

+0

我知道ajax_event.php正在工作 - 同一個HTML和js文件上的其他jquery使用完全相同的$ .ajax函數與它進行交互。 – Matt 2010-11-16 16:42:39

回答

4

謝謝大家的回覆。

我最終包裝的我在下面的代碼原有代碼Ajax調用部分:

if($('#form_scheduleEvent').valid() == true){ 
    //Process ajax request... 
} 
return false; 
5

你應該將你的AJAX提交通話將被提交回調的驗證插件發射了:

$('#form_scheduleEvent').validate({ 
rules: { 
    name : {required: true, maxlength: 45}, 
    address : {required: true, maxlength: 45}, 
    phone : "required", 
    submitHandler: function(form) { DoSubmit(); } 
} 
}); 


function DoSubmit(){ 
    $.ajax({ 
     type: "POST", 
     url: "common/ajax_event.php", 
     data: formSerialized, 
     timeout:3000, 
     error:function(){alert('Error');}, 
     success: function() {alert('It worked!');} 
    } 
+0

感謝您的回答。我也嘗試了這些代碼,但是在我給詹姆斯的回覆中提到了同樣的錯誤。 – Matt 2010-11-16 19:28:25

1

正如Grillz說你提交併單獨確認,你應該在你的點擊事件中明確地調用驗證來執行它。

或反之亦如Paddy顯示! ;)

+1

知道我應該回答! – 2010-11-16 16:44:42

+0

感謝您的回答! – Matt 2010-11-16 19:28:54

2

您在驗證檢查之外調用表單提交。你可能會想要這樣的事情:

$('#form_scheduleEvent').validate({ 
rules: { 
    name : {required: true, maxlength: 45}, 
    address : {required: true, maxlength: 45}, 
    phone : "required", 
    success: function() { 
     $.ajax({ 
     type: "POST", 
     url: "common/ajax_event.php", 
     data: formSerialized, 
     timeout:3000 
     } 
    } 
}); 
+0

感謝您的回答!我已經得到了如下所示的確切代碼,除了$ .ajax部分。我用alert('test')取代了它;因爲我仍然無法得到這個工作。表單IS驗證(錯誤消息顯示字段是否完整),但它只是提交頁面而不顯示警報框....還有什麼想法? – Matt 2010-11-16 19:27:33

+0

我將formHandler更改爲「成功」。 – 2010-11-16 21:22:30

0
​​

提交處理程序應該是外面的規則。 :-)