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;
}
}
}
mvc注解验证的更多相关文章
- C# asp.net mvc 注解验证
看代码,看注解,看懂了单词,没看懂意思. 今日只能专攻一下这项特性. 1.Remote 在看这个例子的时候 ,看了JsonResult 以及 JsonRequestBehavior.AllowGet解 ...
- asp.net mvc 模型验证注解,表单提交
一.添加模型 public class Account { public int ID { get; set; } [Display(Name = "姓名")] //设置要显示的字 ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- MVC的验证(模型注解和非侵入式脚本的结合使用)
@HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客户端,后台服务器的验证,MVC统统都做了包含,即使用户 ...
- MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式
MVC的验证(模型注解和非侵入式脚本的结合使用) @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...
- MVC 数据验证
MVC 数据验证 前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解.System.ComponentModel.DataAnnotations 一.基础特性 一.Required 必填 ...
- MVC 数据验证[转]
前一篇说了MVC数据验证的例子,这次来详细说说各种各样的验证注解. 一.基础特性 一.Required 必填选项,当提交的表单缺少该值就引发验证错误. 二.StringLength 指定允许的长度 指 ...
- MVC之验证
MVC之验证 有时候我觉得,很多人将一个具体的技术细节写的那么复杂,我觉得没有必要,搞得很多人一头雾水的,你能教会别人用就成了,具体的细节可以去查MSDN什么的,套用爱因斯坦的名言:能在网上查到的就不 ...
- spring mvc: Hibernate验证器(字段不能为空,在1-150自己)
spring mvc: Hibernate验证器(字段不能为空,在1-150自己) 准备: 下载Hibernate Validator库 - Hibernate Validator.解压缩hibern ...
随机推荐
- Django 的ORM
指定字段: <1> CharField:字符串字段,用于较短的字符串,CharField 要求必须有一个参数 maxlength,用于从数据库层和Django效验层限制该字段所允许的最大字 ...
- MUI 支付宝支付接入
沙箱测试地址:https://openhome.alipay.com/platform/appDaily.htm 1资源下载地址:https://docs.open.alipay.com/54/106 ...
- appium在android7.0上无法启动问题
前言 由于最近很多android手机升级到7.0系统了,有些小伙伴的appium版本用的还是1.4版本,在运行android7.0的app自动化时候遇到无法启动问题:WebDriverExceptio ...
- java代码-----实现打印三角形
总结:今天我有个体会,喜欢不代表了解,了解不代表精通.我好失败 对于正三角形,就是注意空格.打星号.的实现. package com.a.b; public class Gl { public sta ...
- 阻塞队列之一:BlockingQueue汇总
一.阻塞队列介绍 BlockingQueue 通常用于一个线程生产对象,而另外一个线程消费这些对象的场景.下图是对这个原理的阐述: 一个线程往里边放,另外一个线程从里边取的一个 BlockingQue ...
- Git回版本回退
这里我们使用命令行的方式对已经提交的版本进行强行回退操作~~~ 一.将git的安装目录bin放到path路径中, 如下图所示: 二.进入cmd界面,依次输入下面内容即可(git 远程仓库 回退到指定版 ...
- Java--神奇的hashcode
一.Object的HashCode定义 public native int hashCode(); Object类的hashCode方式使用了native修饰也就意味着真正的实现调用的其他语言编写的方 ...
- 「小程序JAVA实战」小程序和后台api通信(28)
转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxuhehoutaiapitongxin28/ 开发最重要的就是实操! 小 ...
- Java后端发送email实现
依赖的jar包 <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail& ...
- MySQL group_concat_max_len
MySQL提供的group_concat函数可以拼接某个字段值成字符串,如 select group_concat(user_name) from sys_user,默认的分隔符是 逗号,即" ...