【发布时间】:2011-11-11 16:40:56
【问题描述】:
我正在尝试对 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 显示为文本“例如传真 n°”,它“认为”我输入了无效输入,因为该文本由字母组成,而不仅仅是数字。
知道怎么解决吗?
提前致谢!吉列尔莫。
【问题讨论】:
标签: jquery asp.net asp.net-mvc jquery-plugins customvalidator