博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc注解验证
阅读量:7222 次
发布时间:2019-06-29

本文共 3311 字,大约阅读时间需要 11 分钟。

前端:

@{

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

转载于:https://www.cnblogs.com/kexb/p/4745911.html

你可能感兴趣的文章
Facebook智能bug修复神器:让程序员少掉几根头发
查看>>
雪球CTO王栋谈招聘:认可团队与产品最重要
查看>>
Atlassian是怎样进行持续交付的?且听 Steve Smith一一道来
查看>>
通过Baratine将Lucene库暴露为微服务
查看>>
SQL Server 2016:伸展数据库
查看>>
宜人贷CTO段念:我与“研发管理”
查看>>
CentOS6 编译安装 redis-3.2.3
查看>>
Web Storage相关
查看>>
关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
查看>>
分布式任务队列Celery
查看>>
[PHP内核探索]PHP中的哈希表
查看>>
WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
查看>>
【css3】浏览器内核及其兼容性
查看>>
JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
查看>>
Linux Process Manage
查看>>
JS中的事件绑定,事件捕获,事件冒泡以及事件委托,兼容IE
查看>>
Android入门及效率开发
查看>>
Apache-drill Architechture
查看>>
JQuery - Sizzle选择器引擎原理分析
查看>>
打造最美HTML5 3D机房(第三季新增资产管理、动环监控)
查看>>