2011-04-19 96 views
1

我想實現什麼,我們可以在這裏看到:jQuery的valiation:使用遠程驗證

http://weblogs.asp.net/cibrax/archive/2008/08/01/combining-jquery-validation-with-asp-net-mvc.aspx

我遇到的問題是,它不使用遠程驗證。

<form action="<%=Url.Action("Register", "Home")%>" method="post" id="form-sign-up">

在我而言,每次我點擊提交,其張貼到註冊行動 - 我似乎從來沒有擊中JSON驗證方法。事實上,我下載了示例,遠程帖子不適合我。遠程部分是否在樣本中爲其他人工作?

有誰知道如何讓他們的工作或有一個更好的例子嗎?

基本上我有是一個模式彈出編輯表單。我想發佈到保存(或在這種情況下Register)方法之前做的所有驗證。

我只需要檢查是必要的,遠程驗證是檢查它是否是重複的。在我的例子中,我錯過了驗證器並最終進入了保存方法。很明顯,我只想保存字段是否填寫,而不是重複。


編輯0:

我使用MVC 2

<form action="/Region/Save" method="post" id="Form-AddRegion"> 
    <div class="editor-label"> 
     <%: Html.Label("Region:") %> 
     <%: Html.TextBoxFor(model => model.Region, new { @class = "edit required remote"}) %> 
    </div> 

    <p> 
     <input type="submit" value="Save" /> 
     <input type="button" onClick="window.parent.CloseWindow();" value="Cancel" /> 
    </p> 
</form> 

public ActionResult ValidateRegion(string name) 
{ 
    if (!something) 
     return Json(true, JsonRequestBehavior.AllowGet); 
    return Json(false, JsonRequestBehavior.AllowGet); 
} 

<script type="text/javascript"> 

    $(document).ready(function() { 

     alert('jQuery is referenced properly'); 

     $("#Form-AddRegion").validate({ 
      rules: 
      { 
       //rules each for specified input-id 
       Region: 
       { 
        required: true, 
        remote: '<%=Url.Action("ValidateRegion","Region") %>' 
       } 
      }, 
      messages: 
      { 
       Region: 
       { 
        required: "Please provide a Region", 
        remote: jQuery.format("{0} is already in use") 
       } 
      } 
     }); 
    }); 
</script> 

所以問題是,在提交,我一直張貼到保存方法/Region/Save驗證ValidateRegion之前!我錯過了global.asax.cs的路線嗎?

我需要知道什麼時候的JavaScript代碼後/進入驗證方法?我會假設提交,但它似乎永遠不會被調用!?

當人們沒有閱讀你的問題,並指出你在主要的文檔參考中,肯定是討厭的,有趣的是,我已經在那裏。

+0

你使用的是什麼版本的mvc? PLZ提供一點代碼 – 2011-04-19 07:44:26

回答

0

任何一種形式驗證必須連接到的onsubmit功能。如果這個函數將返回假表不會所需提交。

<form onSubmit="function(){return false;}"> 

永遠所需提交

連接JQuery驗證到的onsubmit是通過驗證完成()函數NEEDS要在文件準備

還有一兩件事都輸入NEEDS完成可以驗證的名稱屬性

更多文檔和大量示例可以在這裏找到http://docs.jquery.com/Plugins/Validation/

編輯

這是的jsfiddle代碼: http://jsfiddle.net/fzf3q/1/

它的工作原理有隻sugesstion是確保驗證函數包含只是用它運行之前:

alert($("#Form-AddRegion").validate)