ASP.NET MVC内置六大验证特性

我与影子孤独终老i 提交于 2020-01-10 04:57:46
  1. 引用/Scripts/jquery-1.10.2.min.js

  2. 引用/Scripts/jquery.validate.min.js

  3. 引用/Scripts/jquery.validate.unobtrusive.min.js
    Model实体类///
    /// Model实体类
    ///
    public class UserRegistModel
    {

    /*
    * 1、Required 必填(默认为不允许为空。允许为空则添加AllowEmptyStrings = true)
    * 2、StringLength 长度
    * 3、Compare 比较
    * 4、RegularExpression 正则
    * 5、Range 范围
    * 6、Remote 回调
    * **/

 //必填
    [Required(AllowEmptyStrings = false,ErrorMessage ="用户名不能为空")]
    public string UserName { get; set; }

    //必填,长度不超20
    [Required]
    [StringLength(20, ErrorMessage = "设置的密码不能超过20个字符")]
    public string Password { get; set; }

    //必填,两次密码要一致
    [Required]
    [Compare("Password",ErrorMessage ="两次密码不一致")]
    public string ConfirmPassword { get; set; }

    //必填,且是邮箱模式   (@@"[A-Za-z0-9._%+-]+@@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"))
    [Required]
    [RegularExpression(@@"^\w+([-+.]\w+)*@@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "邮箱格式不正确")]
    public string Email { get; set; }

    //必填,年龄15-30
    [Required]
    [Range(15,30,ErrorMessage ="年龄不符合")]
    public int Age { get; set; }
        
}
  1. 表单代码
@using (Html.BeginForm("您的Action", "您的控制器", FormMethod.Post, new { @class = "form-horizontal" }))
{
    <div class="form-group row">
        @Html.Label("用户名:", new { @class = "col-sm-2" })
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control form-control-success" })
            <small class="form-text">@Html.ValidationMessageFor(m => m.UserName)</small>
        </div>
    </div>
    <div class="form-group row">
        @Html.Label("密码:", new { @class = "col-sm-2" })
        <div class="col-sm-10">
            @Html.Password("Password","", new { @class = "form-control" })
            <small class="form-text">@Html.ValidationMessageFor(m => m.Password)</small>
        </div>
    </div>
    <div class="form-group row">
        @Html.Label("确认密码:", new { @class = "col-sm-2" })
        <div class="col-sm-10">
            @Html.Password("ConfirmPassword", "", new { @class = "form-control" })
            <small class="form-text">@Html.ValidationMessageFor(m => m.ConfirmPassword)</small>
        </div>
    </div>
    <div class="form-group row">
        @Html.Label("邮件:", new { @class = "col-sm-2" })
        <div class="col-sm-10">
            @Html.TextBox("Email", "", new { @class = "form-control" })
            <small class="form-text">@Html.ValidationMessageFor(m => m.Email)</small>
        </div>
    </div>
    <div class="form-group row">
        @Html.Label("年龄:", new { @class = "col-sm-2" })
        <div class="col-sm-10">
            @Html.TextBox("Age", "", new { @class = "form-control" })
            <small class="form-text">@Html.ValidationMessageFor(m => m.Age)</small>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-10 offset-sm-2">
            <input type="submit" value="提交" class="btn btn-primary" />
        </div>
    </div>
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!