ASP.NET MVC 3 Validation - 正则表达式验证RegularExpressionAttribute之日期验证
http://blog.csdn.net/jackvs/article/details/6701186
C#中Dictionary的用法
http://jingyan.baidu.com/article/9989c7460ab872f648ecfeed.html
String.Format格式说明
http://www.cnblogs.com/tuyile006/archive/2006/07/13/449884.aspx
《Pro ASP.NET MVC 3 Framework》学习笔记之三十一 【模型验证】
http://www.cnblogs.com/mszhangxuefei/archive/2012/05/28/mvcnotes_31.html
HTML5 Form Validation Examples
http://www.the-art-of-web.com/html/html5-form-validation/
jQuery Validation Plugin
http://www.the-art-of-web.com/html/html5-form-validation/
1.ModelState.IsValid用来判断model是否通过验证,前台显示如下
@Html.ValidationSummary()
@Html.ValidationMessageFor(model => model.TestName)
2.ModelState 是个Dictionary类型
ModelState.AddModelError("testName","* Test Name is not valid.");
ModelState["TestName"].Errors.Count()
3.DateTime
[DataType(DataType.DateTime,ErrorMessage="Please input date value")] // ErrorMessage is not valid
public DateTime TestTime { get; set; }
DataType后边的Error Message不起作用,不能覆盖生成元素的data-val-date/data-val-number属性,可能对于特殊字符串email,url类型的比较有用
[Required(ErrorMessage = "Please input a date value")] 也无效
[RegularExpression(@"^(?:(?!0000)[0-9]{4}(-|/)(?:(?:0[1-9]|1[0-2])(-|/)(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])(-|/)(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)(s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])?$", ErrorMessage = "Please input a date(YYYY/MM/DD) value")] 有效
但这样比较累赘,还有个简单替换生成元素属性的方法,将-替换成_就可以代替自动生成的信息了
@Html.TextBoxFor(m => m.Test, new { @data_val_date="please input a valid date"})
这样我们可以直接添加下列属性来实现客户端的认证
data_val_number,data_val_date
data_val_email 无效
data_val_required
data-val-regex-pattern,data-val-regex
data-val-length-min,data-val-length-max,data-val-length
data-val-range-min,data-val-range-max,data-val-range
data-val-remote-url,data-val-remote-type,data-val-remote-additionalfields,data-val-remote 无效
有的时候很奇怪,只对@Html.EditorFor(m => m.Test)有效,却对@Html.TextBoxFor(m => m.Test)无效,属性不能正确生成
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
这种写法只对@Html.DisplayFor(mbox=>mbox.Test)和@Html.EditorFor(mbox=>mbox.Test)才有效,
对于@Html.LabelFor(mbox=>mbox.Test)和@Html.TextBoxFor(mbox=>mbox.Test)无效
4.String
[Display(Name = "Test Name")]
[Required(ErrorMessage = "Please input the test name.")]
[StringLength(10,ErrorMessage="The length can't longer than 10")]
[StringLength(10, MinimumLength=3)]
[RegularExpression(@"([a-g])*", ErrorMessage = "Please input valid character")]
public string TestName { get; set; }
<input name="Test Name" class="text-box single-line" id="Test" type="text" data-val="true" data-val-regex-pattern="([a-g])*" data-val-regex="valid character" data-val-length-min="3" data-val-length-max="10" data-val-length="length should be 3~10" value=""/>
[DataType(DataType.Password)]
public string Test { get; set; }
<input name="Test" class="text-box single-line valid" id="Test" type="password" value=""/>
5.Number
[Range(1, 100, ErrorMessage = "Please input 1~100.")]
public int Test { get; set; }
<input name="Test" class="text-box single-line" id="Test" type="number" data-val-required="Test is required" data-val="true" data-val-number="Test is not a valid number" data-val-range-min="1" data-val-range-max="100" data-val-range="input 1~100" value="0"/>
6.Enum Type
public enum eReorderLvl
{
five = 5,
ten = 10
}
[EnumDataType(typeof(eReorderLvl), ErrorMessage = "Please input 5,10")]
public int Test { get; set; }
7.Bool
[Range(typeof(bool), "true", "true", ErrorMessage="你必须接受条款")]
public bool TermsAccepted { get; set; }
<input name="Test" class="check-box" id="Test" type="checkbox" data-val-required="Test is required" data-val="true" data-val-range-min="True" data-val-range-max="True" data-val-range="must true" value="true"/>
8.以上是属于System.ComponentModel.DataAnnotations,也可以实时验证,使用System.Web.Mvc.RemoteAttribute
[Remote("CheckIfExist", "TestName", HttpMethod = "POST", AdditionalFields = "TestId", ErrorMessage = "Test Name already exists.")]
public string TestName { get; set; }
<input name="TestName" class="required" id="TestName" type="text" data-val="true" data-val-remote-url="/Test/CheckIfExist" data-val-remote-type="POST" data-val-remote-additionalfields="*.TestName,*.TestId" data-val-remote="Test Name already exists" data-fieldname="TestName"
value="aaa"/>
9.如果想为基本的类型也在客户端实时验证,可以使用
jquery.unobtrusive-ajax.js
jquery.validate.js
Jquery.validate.unobtrusive.js
<appSettings>
...
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>