2010-07-28 41 views
2

我有這樣的代碼,表單提交..驗證一個表單輸入不爲空

<input type="submit" runat="server" id="buttonSubmit" value="Add" style="width:100px;" /> 

我BeginForm是這樣的..

<% using (Html.BeginForm("Insert", "StudentController", FormMethod.Post, new { @id = "exc-" })) 
     {%> 

我有我的觀點,我需要一個文本框檢查我的文本框是否爲空或如果它是空的顯示警告框說請在文本框中輸入一些值 其他明智去控制器..

請任何機構幫我嗎?

感謝

回答

4

你可以做到這一點的方法很多,但可能是最乾淨的是在您的視圖模型使用的數據註釋。例如 -

public class MyViewModel 
{ 
    [Required] 
    public string MyProperty { get; set; } 
} 

現在,在您查看使用

<% Html.EnableClientValidation(); %> 

啓動形式之前。這將導致JavaScript對象在發送給客戶端的標記中發出。該腳本看起來像該例子中

<script type="text/javascript"> 
//<![CDATA[ 
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } 
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"FirstName","ReplaceValidationMessageContents":true,"ValidationMessageId":"FirstName_validationMessage","ValidationRules":[{"ErrorMessage":"The First Name field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"LastName","ReplaceValidationMessageContents":false,"ValidationMessageId":"LastName_validationMessage","ValidationRules":[{"ErrorMessage":"The Last Name field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"EmailAddress","ReplaceValidationMessageContents":false,"ValidationMessageId":"EmailAddress_validationMessage","ValidationRules":[{"ErrorMessage":"The Email Address field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"ZipCode","ReplaceValidationMessageContents":false,"ValidationMessageId":"ZipCode_validationMessage","ValidationRules":[{"ErrorMessage":"Zip Code must be 5 character long.","ValidationParameters":{"minimumLength":0,"maximumLength":5},"ValidationType":"stringLength"},{"ErrorMessage":"Zip Code must be five digits.","ValidationParameters":{"pattern":"\\d{5}"},"ValidationType":"regularExpression"},{"ErrorMessage":"The Zip Code field is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false,"ValidationSummaryId":"valSumId"}); 
//]]> 
</script> 

該對象包含能夠由客戶端驗證插件被用來掛接驗證在客戶端驗證元數據。 ASP.NET MVC 2附帶的插件是Microsoft AJAX驗證程序,您需要在頁面中包含這些腳本才能使用驗證(按順序爲MicrosoftAjax.jsMicrosoftMVCAjax.jsMicrosoftMvcValidation.js)。

或者,如果您對jQuery更加熟悉,可以在MvcFutures源代碼中獲取一個腳本,該腳本將驗證掛鉤到jQuery驗證插件中(這不是一個完全成熟的腳本,並且缺少幾個部分,例如作爲獲取客戶端驗證摘要)。該腳本爲MicrosoftMvcJQueryValidation.js,您可以獲得它here

使用數據註釋的優點是您也可以獲得服務器端驗證,您的客戶端和服務器端驗證將驗證期望值。此外,數據註釋允許您從屬性中設置字段標籤的錯誤消息和名稱(錯誤消息和顯示名稱*也可以來自資源文件)

*因爲MVC2是針對.NET 3.5版本的數據編譯的註釋,顯示名稱不能從資源文件中設置。這是一個解決方法 - DisplayName attribute from Resources?


現在的EASY WAY


剛剛成立的形式

var form = document.getElementById('exc-'); 
var oldSubmit = form.onsubmit || function() {}; 
form.onsubmit = function() { 
    var input = document.getElementById('myinput'); 
    if (input.value === '') { 
     alert('please Enter some value in textbox'); 
     return false; 
    } 
    oldSubmit(); 
} 

或使用jQuery

$('#exc-').submit(function() { 
    if ($('#myinput').val() === '') { 
     alert('please Enter some value in textbox'); 
     return false; 
    }    
}); 
在提交事件處理程序