mvc3+jquery validation怎么验证日期
2个回答
展开全部
在ASP.NET MVC 3.0中,jQuery验证框架被引入是我们可以采用Unobtrusive JavaScript的方式进行客户端验证。
接下来我们通过Visual Studio的ASP.NET MVC项目模板创建验证日期的Web应用,这样做有两个目的:
其一、项目在创建过程中会自动添加包含jQuery本身及其验证插件的.js文件;
其二,可以确保我们现在使用的用于验证的.js文件和ASP.NET MVC真正使用的.js文件是一致的。
先创建一个AgeRangeAttribute类,文件名: AgeRangeAttribute.cs 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace CustomClientValidation
{
public class AgeRangeAttribute : RangeAttribute, IClientValidatable
{
public AgeRangeAttribute(int minimum, int maximum)
: base(minimum, maximum)
{ }
public override bool IsValid(object value)
{
DateTime birthDate = (DateTime)value;
DateTime age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks);
return age.Year >= (int)this.Minimum && age.Year <= (int)this.Maximum;
}
public override string FormatErrorMessage(string name)
{
return base.FormatErrorMessage("年龄");
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule validationRule = new ModelClientValidationRule(){ ValidationType = "agerange", ErrorMessage= FormatErrorMessage(metadata.DisplayName)};
validationRule.ValidationParameters.Add("currentdate",DateTime.Today.ToString("dd-MM-yyyy"));
validationRule.ValidationParameters.Add("minage",this.Minimum);
validationRule.ValidationParameters.Add("maxage",this.Maximum);
yield return validationRule;
}
}
}Views层 Views/Home/Index.cshtml代码如下:
@model CustomClientValidation.Models.Person
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="Save" />
}Models层 /Models/Person.cs 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace CustomClientValidation.Models
{
public class Person
{
[DisplayName("姓名")]
public string Name { get; set; }
[AgeRange(18, 30, ErrorMessage = "{0}必须在{1}和{2}之间!")]
[DisplayName("出生日期")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime? BirthDate { get; set; }
}
}Controllers层 Controllers/HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CustomClientValidation.Models;
namespace CustomClientValidation.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Person{ BirthDate = DateTime.Today, Name = "Foo"});
}
[HttpPost]
public ActionResult Index(Person person)
{
return View(person);
}
}
}
接下来是客户端的js文件代码
Scripts/cutom-validation.js
jQuery.validator.addMethod("agerange",
function (value, element, params) {
var minAge = params.minage;
var maxAge = params.maxage;
var literalCurrentDate = params.currentdate;
var literalBirthDate = value;
var literalCurrentDates = literalCurrentDate.split('-');
var literalBirthDates = literalBirthDate.split('-');
var birthDate = new Date(literalBirthDates[2], literalBirthDates[1], literalBirthDates[0]);
var currentDate = new Date(literalCurrentDates[2], literalCurrentDates[1], literalCurrentDates[0]);
var age = currentDate.getFullYear() - birthDate.getFullYear();
return age >= minAge && age <= maxAge
});
jQuery.validator.unobtrusive.adapters.add("agerange", ["currentdate", "minage", "maxage"], function (options) {
options.rules["agerange"] = {
currentdate: options.params.currentdate,
minage: options.params.minage,
maxage: options.params.maxage
};
options.messages["agerange"] = options.message;
});
运行程序,效果图如下:
如果需要完整工程方案包代码可以分享。
展开全部
要用jquery中的rule add函数的,因为本来已生成了一些规则,我反正成功了,不懂还可以问我
function validateForm() {
return $('#createForm').validate({
rules: {
remark: { required: true }
},
messages: {
remark: { required: "xxx" }
}
}).form();
}
function validateForm() {
return $('#createForm').validate({
rules: {
remark: { required: true }
},
messages: {
remark: { required: "xxx" }
}
}).form();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询