前端:

@{
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;
}
}

}

最新文章

  1. ACM题目————滑雪
  2. (转)RabbitMQ消息队列(九):Publisher的消息确认机制
  3. 查看局域网内某个ip的mac地址
  4. matlab 学习
  5. Ubuntu lnmp安装记录
  6. nginx服务器,php-fpm重启
  7. BZOJ 1049 数字序列
  8. 什么是 Terminal
  9. webclient下载文件 带进度条
  10. HDU 2073 无限的路
  11. jieba库与好玩的词云的学习与应用实现
  12. 爬虫之scrapy--基本操作
  13. hbaes之createTable执行流程
  14. 使用GOOGLE COLAB训练深度学习模型
  15. 如何将maven的jar项目简单快速的转变成war项目
  16. QQ通信机制(转)
  17. Oracle 11g EM删除重建的方法
  18. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
  19. laravel本地开发的项目部署到服务器上
  20. GraphQL和C#

热门文章

  1. .NET的URL重写
  2. jmeter数据关联_后置处理器_正则表达式提取器
  3. 杂项-数学软件:Mathematica
  4. python学习(二十二) Python 中boolean
  5. SQL Server Management Studio (SSMS)
  6. Http协议是有状态的还是无状态的???
  7. 搭建psdash 监控系统
  8. &lt;转&gt;Python3.x和Python2.x的区别
  9. Python 小知识点(7)--类的创建方式
  10. oracle常用函数总结(二)