验证表单输入不为空

时间:2010-07-28 20:37:39

标签: asp.net asp.net-mvc validation

我有表格提交的代码..

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

我的BeginForm是这样的..

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

我的视图中有一个文本框我需要检查我的文本框是否为空如果是空显示警告框说请在文本框中输入一些值 其他明智的去控制器..

请有人帮帮我吗?

感谢

1 个答案:

答案 0 :(得分:4)

您可以通过多种方式执行此操作,但最简洁的方法是在ViewModel上使用数据注释。例如 -

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 validate插件中(这不是一个完全成熟的脚本,并且缺少一些部分,例如获取客户端验证摘要)。该脚本为MicrosoftMvcJQueryValidation.js,您可以获取here

使用数据注释的优点是您也可以获得服务器端验证,并且您的客户端和服务器端验证将验证预期值。此外,数据注释允许您从属性中设置字段标签的错误消息和名称(错误消息和显示名称*也可以来自资源文件)

*因为MVC2是针对.NET 3.5版本的数据注释编译的,所以无法从资源文件中设置显示名称。有一种解决方法 - DisplayName attribute from Resources?


现在很容易


只需在表单

上设置提交事件处理程序即可
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;
    }              
});