我使用.net 4與mvc 2和註釋進行驗證。我建立了一個自己的validationAttribute來檢查我的數據庫中是否存在一個電子郵件。 c#函數ismailexisting()返回正確的值,但JavaScript不能正確執行。的JavaScript/jQuery的部分看起來像這樣:.net mvc jquery clientside驗證問題
Sys.Mvc.ValidatorRegistry.validators["isexisting"] = function (rule) {
return function (value, context) {
$.ajax({
url: "/persons/ismailexisting",
type: "POST",
data: { email: value },
success: function (data) {
//alert("success: " + data);
if (data == "yes") {
return false;
}
}
})
return true;
return rule.ErrorMessage;
};
};
如果電子郵件已經存在,函數ismailexisting()返回「是」,否則「不」。 如果存在電子郵件(因此數據爲「是」),則JavaScript應阻止用戶繼續,因爲他必須輸入其他電子郵件。 如果我取消註釋alert(),則數據的值是正確的。但有些東西讓JavaScript遠離我提供的正確結果。
所有其他的東西進行自定義驗證是否正確實現,因爲我已經實現了幾個其他自定義的驗證。
在此先感謝!
升級到MVC3的任何機會呢? MVC中的'RemoteAttribute'完全正是你想要完成的。 – tvanfosson