添加 MVC3 不显眼的验证 (addMinMax)
我有这样的属性:
public class AgeRangeAttribute:ValidationAttribute,IClientValidatable
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessage,
ValidationType = "agerange"
};
if (MinAge != int.MinValue)
{
rule.ValidationParameters.Add("minage", DateTime.Now.AddYears(-MinAge).ToShortDateString());
}
if(MaxAge != int.MinValue)
{
rule.ValidationParameters.Add("maxage", DateTime.Now.AddYears(-MaxAge).ToShortDateString());
}
yield return rule;
}
}
和 JS 验证器表示:
jQuery.validator.addMethod("agerangemin", function (value, element, param) {
var date = setDate(new Date(),value);
var expected = setDate(new Date(), param);
return date <= expected;
});
jQuery.validator.addMethod("agerangemax", function (value, element, param) {
var date = setDate(new Date(), value);
var expected = setDate(new Date(), param);
return date > expected;
});
jQuery.validator.addMethod("agerangemaxmin", function (value, element, param) {
var date = setDate(new Date(), value);
var expectedMin = setDate(new Date(), param[1]);
var expectedMax = setDate(new Date(), param[0]);
return date <= expectedMin && date > expectedMax;
});
jQuery.validator.unobtrusive.adapters.addMinMax("agerange", "agerangemin", "agerangemax", "agerangemaxmin");
并且这个 js 不起作用(没有例外......可能已经死了&:))
但这工作正常:
jQuery.validator.addMethod("agerange", function (value, element, param) {
var date = new Date();
date = setDate(date, value);
var expected = setDate(new Date, param);
return date <= expected;
});
jQuery.validator.unobtrusive.adapters.addSingleVal("agerange", "minage");
它有什么问题?哪里有错误?
I have such attribute:
public class AgeRangeAttribute:ValidationAttribute,IClientValidatable
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessage,
ValidationType = "agerange"
};
if (MinAge != int.MinValue)
{
rule.ValidationParameters.Add("minage", DateTime.Now.AddYears(-MinAge).ToShortDateString());
}
if(MaxAge != int.MinValue)
{
rule.ValidationParameters.Add("maxage", DateTime.Now.AddYears(-MaxAge).ToShortDateString());
}
yield return rule;
}
}
and JS validator representation:
jQuery.validator.addMethod("agerangemin", function (value, element, param) {
var date = setDate(new Date(),value);
var expected = setDate(new Date(), param);
return date <= expected;
});
jQuery.validator.addMethod("agerangemax", function (value, element, param) {
var date = setDate(new Date(), value);
var expected = setDate(new Date(), param);
return date > expected;
});
jQuery.validator.addMethod("agerangemaxmin", function (value, element, param) {
var date = setDate(new Date(), value);
var expectedMin = setDate(new Date(), param[1]);
var expectedMax = setDate(new Date(), param[0]);
return date <= expectedMin && date > expectedMax;
});
jQuery.validator.unobtrusive.adapters.addMinMax("agerange", "agerangemin", "agerangemax", "agerangemaxmin");
and this js doesn't work (no exceptions... may be dead& :) )
but this works fine:
jQuery.validator.addMethod("agerange", function (value, element, param) {
var date = new Date();
date = setDate(date, value);
var expected = setDate(new Date, param);
return date <= expected;
});
jQuery.validator.unobtrusive.adapters.addSingleVal("agerange", "minage");
Whats wrong with it? Where is mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一年前,我在自定义不引人注目的验证方面遇到了一些困难。这是我想出的。我需要创建验证器来检查用户是否年满十八岁。
我的代码:
在模型中:
CustomDateValidation.js 中的一些 java 脚本代码:
在视图中,我渲染文本框如下:
最后一个是我使用的脚本:
I have some difficulties with custom unobtrusive validation a year ago. Here is what I came up with. I needed to create validator that check if user is eighteen years old.
My code:
In model:
And some java script code in CustomDateValidation.js:
In view I render textbox like that:
The last one is scripts I use: