2011-04-04 57 views
0

我正在使用JörnZaefferer的JQuery驗證,但我需要進行一些數據庫調用來驗證某些字段(例如檢查用戶名是否唯一)。這是可能使用這個插件,如果是這樣,有人有一些語法的例子嗎?這裏是我當前的代碼:JQuery驗證和數據庫調用

$("form").validate({ 
      rules: { 
       txtUserName: { 
        required: true, 
        minlength: 4 
       }, 
       txtPassword: { 
        required: true 
       }, 
       txtConfirmPassword: { 
        required: true, 
        equalTo: "#txtPassword" 
       }, 
       txtEmailAddress: { 
       required: true, 
       email: true 
       }, 
       txtTelephoneNumber: { 
        required: true, 
        number: true 
       } 
      }, 
      messages: { 
       txtUserName: { 
        required: "Please enter a User Name", 
        minlength: "User Name must be at least 4 characters" 
       }, 
       txtPassword: { 
        required: "Please enter a Password" 
       }, 
       txtConfirmPassword: { 
        required: "Please confirm Password", 
        equalTo: "Confirm Password must match Password" 
       }, 
       txtEmailAddress: { 
        required: "Please enter an Email Address", 
        email: "Please enter a valid Email Address" 
       }, 
       txtTelephoneNumber: { 
        required: "Please enter a Telephone Number", 
        number: "Telephone Number must be numeric" 
       } 
      } 
     }); 
    }); 

編輯:

我已經走到這一步,但我這樣做的時候,我失去了我的窗體上的值,大概是因爲格式已經張貼在這裏?

$("form").validate({ 
    //errorLabelContainer: $("#divErrors"), 

     rules: { 
      txtUserName: { 
       required: true, 
       minlength: 4 
      }, 
      txtPassword: { 
       required: true 
      }, 
      txtConfirmPassword: { 
       required: true, 
       equalTo: "#txtPassword" 
      }, 
      txtEmailAddress: { 
       required: true, 
       email: true 
      }, 
      txtTelephoneNumber: { 
       required: true, 
       number: true//, 
       //postalCode:true 
      } 
     }, 
     messages: { 
      txtUserName: { 
       required: "Please enter a User Name", 
       minlength: "User Name must be at least 4 characters" 
      }, 
      txtPassword: { 
       required: "Please enter a Password" 
      }, 
      txtConfirmPassword: { 
       required: "Please confirm Password", 
       equalTo: "Confirm Password must match Password" 
      }, 
      txtEmailAddress: { 
       required: "Please enter an Email Address", 
       email: "Please enter a valid Email Address" 
      }, 
      txtTelephoneNumber: { 
       required: "Please enter a Telephone Number", 
       number: "Telephone Number must be numeric" 
      } 
     }, 
     onValid: addUser() 
    }); 
}); 

功能ADDUSER(){

alert($('input[name="txtUserName"]').val()); 

}

+0

檢查尼克的回答是> http://stackoverflow.com/questions/3650909/jquery-validate/3650937#3650937 – neebz 2011-04-04 09:59:14

+0

看着這個線程後,我想這一點,但我的web服務似乎並不被稱爲: – user517406 2011-04-04 10:14:23

+0

能你添加你的代碼?客戶端和服務器端 – neebz 2011-04-04 10:15:30

回答

1

有您使用一個遠程規則。您可以指定一個POST URL,然後檢查數據並返回true/false。看看它的文檔瞭解更多細節。這正是你的場景

0

JQuery驗證插件對輸入的每一個變化和注意力進行驗證。我不認爲調用Web服務和檢查數據庫是每一次重點領域變化的主意都不是好主意。相反,你可以通過表單提交例如ajax來完成。

0

您可以使用ajax調用從數據庫檢索數據,然後使用這些數據,您可以使用遠程規則處理驗證。