ASPNET MVC 3 不显眼且客户端验证无法与 placeHolder jQuery 插件一起使用
我正在尝试对 ASPNET MVC 3 项目和 placeHolder jQuery 插件使用不显眼的验证,如下所示:
[StringLength(20)]
[OnlyNumbersValidation(ErrorMessage = " ")]
[DisplayName("Fax n°")]
public string Fax { get; set; }
@Html.TextBoxFor(model => model.Fax, new { @class = "txt-input", placeholder = "Eg. Fax n°", maxlength = 31 })
@Html.ValidationMessageFor(model => model.Fax, "*", new { @class = "redtxt" })
public class OnlyNumbersValidationAttribute : RegularExpressionAttribute, IClientValidatable
{
public OnlyNumbersValidationAttribute()
: base(@"^\d+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
{
var errorMessage = FormatErrorMessage(metadata.GetDisplayName());
yield return new OnlyNumbersValidationRule(errorMessage);
}
}
public class OnlyNumbersValidationRule : ModelClientValidationRule
{
public OnlyNumbersValidationRule(string errorMessage)
{
ErrorMessage = errorMessage;
ValidationType = "digits";
}
}
}
问题是 MVC 3 和 placeHolder 插件之间的交互,因为如果我不输入任何值(并且 placeHolder 显示为文本“Eg.传真 n°” 它“认为”我输入了无效的输入,因为该文本由字母组成,而不仅仅是数字。
知道如何解决它吗?
提前致谢!吉列尔莫。
I'm trying to use unobstrusive validation with ASPNET MVC 3 project and placeHolder jQuery plugin like this:
[StringLength(20)]
[OnlyNumbersValidation(ErrorMessage = " ")]
[DisplayName("Fax n°")]
public string Fax { get; set; }
@Html.TextBoxFor(model => model.Fax, new { @class = "txt-input", placeholder = "Eg. Fax n°", maxlength = 31 })
@Html.ValidationMessageFor(model => model.Fax, "*", new { @class = "redtxt" })
public class OnlyNumbersValidationAttribute : RegularExpressionAttribute, IClientValidatable
{
public OnlyNumbersValidationAttribute()
: base(@"^\d+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
{
var errorMessage = FormatErrorMessage(metadata.GetDisplayName());
yield return new OnlyNumbersValidationRule(errorMessage);
}
}
public class OnlyNumbersValidationRule : ModelClientValidationRule
{
public OnlyNumbersValidationRule(string errorMessage)
{
ErrorMessage = errorMessage;
ValidationType = "digits";
}
}
}
The problem is the interaction between MVC 3 and placeHolder plugin, since if I enter no value (and the placeHolder is shown with the text "Eg. Fax n°" it "thinks" that I entered an invalid input because that text is composed with letters and not only numbers.
Any idea how to fix it?
Thanks in advance! Guillermo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新 placeholder 插件解决了我的问题
Updating placeholder plugin solved my issue
修复了一个涉及在 html 页面中嵌套表单的错误,并将自定义验证器移动到另一个类修复了该问题。
谢谢。
Fixing an error which involved having nested forms in an html page, and moving the custom validator to another class fixed the problem.
Thanks.