MVC4多种类型的验证 [英] MVC4 many types of validation

查看:160
本文介绍了MVC4多种类型的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个视图

@using (Html.BeginForm("RegisterApartmentOwner", "Home", FormMethod.Post, 
                            new { enctype = "multipart/form-data" }))
                            {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(x => x.FirstName, new {placeholder = "Enter Your First Name" })
            @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { placeholder = "Enter Your Last Name"})
            @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password"})
            @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-label">
          @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password Again"})
        @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.MobileNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.MobileNumber, new { placeholder = "Enter Your Mobile Number"})
            @Html.ValidationMessageFor(model => model.MobileNumber)
    </div>
    <input type="submit" value="Register"  class="submit"/>
}

我的问题是验证工作只是当字段为空,但我想验证发现,当两个密码不相等,当移动电话号码不是数字等。我该怎么办讨好?

my problem is that the validation works just when the fields are empty, but i want the validation to discover when the two passwords are not equal, and when the mobilenumber is not numbers and so on. what should i do please?

推荐答案

您应该看一看数据注解的 http://msdn.microsoft.com/en-us/library/dd901590(v = VS.95)的.aspx

You should take a look at data annotations http://msdn.microsoft.com/en-us/library/dd901590(v=vs.95).aspx

这篇关于MVC4多种类型的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆