mvc注解验证
前端:
@{
Layout = null;
}
@using System.Activities.Expressions
@model MvcApplication1.Models.News
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyIndex</title>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.20.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@* <link href="~/Content/themes/base/jquery.ui.datepicker.css" rel="stylesheet" />*@
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />
@* <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />*@
<style type="text/css">
</style>
<script type="text/javascript">
$(function() {
$("#SubDateTime").datepicker();
$("#Title+span").css("color", "red");
$("#No+span").css("color", "red");
//jQuery.validator.addMethod("enforcetrue", function (value, element, param) {
// return value != "";
//});
//jQuery.validator.unobtrusive.adapters.addBool("enforcetrue");
});
</script>
</head>
<body>
@using (Html.BeginForm("MyIndex", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
<span>新闻编号</span>@Html.TextBoxFor(x => x.No)@Html.ValidationMessageFor(x=>x.No)
<span>新闻标题</span> @Html.TextBoxFor(x => x.Title) @Html.ValidationMessageFor(x=>x.Title)
@Html.TextBoxFor(x=>x.SubDateTime,new{@class="datepicker"})
<input type="submit" value="提交"/>
</div>
}
</body>
</html>
model:
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Microsoft.Ajax.Utilities;
namespace MvcApplication1.Models
{
public class News
{
[Required]
[IsCellPhone(ErrorMessage = "{0} 丫的格式根本不正确")]
[Display(Name = "手机号码")]
public string Title { get; set; }
[Required]
public string No { get; set; }
public DateTime SubDateTime { get; set; }
}
}
验证特性类:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Web.Mvc;
namespace MvcApplication1.Models
{
/// <summary>
/// 验证手机号码格式
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class IsCellPhoneAttribute : ValidationAttribute
{
public IsCellPhoneAttribute()
: base("{0} 格式不正确!")//mark1
{
}
/// <summary>
/// 重写验证方法
/// </summary>
/// <param name="value"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var valueAsString = value.ToString();
const string regPattern = @"^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$|^[1][3,5,8][0-9]{9}$";
// @"^1[3|4|5|8][0-9]\d{4,8}$";
//@"^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$|^[1][3,5,8][0-9]{9}$";
if (!Regex.IsMatch(valueAsString, regPattern))
{
var errorMessage = FormatErrorMessage(validationContext.DisplayName);//格式化mark1处的验证信息的{0}占位符 显示的是属性的名称,如果你设置Display(Name="手机号码")那么显示的就是“手机号码”
//var errorMessage = FormatErrorMessage(valueAsString);//如果想把号码显示出来,而不是字段名称,就把参数改成了传过来的手机号码
return new ValidationResult(errorMessage);
}
}
return ValidationResult.Success;
}
}
}