Asp.net mvc 3 自定义RegularExpressionAttribute缺少用于客户端验证的数据val regex模式
我创建了以下自定义RegularExpressionAttributeAsp.net mvc 3 自定义RegularExpressionAttribute缺少用于客户端验证的数据val regex模式,asp.net-mvc-3,asp.net-mvc-4,data-annotations,unobtrusive-validation,Asp.net Mvc 3,Asp.net Mvc 4,Data Annotations,Unobtrusive Validation,我创建了以下自定义RegularExpressionAttribute [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable { public AlphaNumericAttribute()
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" };
}
}
该字段在视图中生成:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.DriverLicenseNumber)
@Html.ValidationMessageFor(m => m.DriverLicenseNumber)
@Html.TextBoxFor(m => m.DriverLicenseNumber)
}
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.SocialSecurityNumber)
@Html.ValidationMessageFor(m => m.SocialSecurityNumber)
@Html.TextBoxFor(m => m.SocialSecurityNumber)
}
这将在我的html输入标记上产生正确的“数据-”验证属性。但是,渲染的标记如下所示:
<input data-val="true" data-val-alphanumeric="Please enter a valid driver's license number." id="DriverLicenseNumber" name="DriverLicenseNumber" type="text" value="" maxlength="20" class="valid">
该字段在视图中生成:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.DriverLicenseNumber)
@Html.ValidationMessageFor(m => m.DriverLicenseNumber)
@Html.TextBoxFor(m => m.DriverLicenseNumber)
}
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.SocialSecurityNumber)
@Html.ValidationMessageFor(m => m.SocialSecurityNumber)
@Html.TextBoxFor(m => m.SocialSecurityNumber)
}
此验证属性正确呈现data val regex和data val regex模式属性:
<input class="SSNMask valid" data-val="true" data-val-regex="Please enter a valid social security number." data-val-regex-pattern="^([0-9]{3}–[0-9]{2}–[0-9]{4})|([ ]{3}–[ ]{2}–[ ]{4})|([0-9]{9,9})$" id="SocialSecurityNumber" name="SocialSecurityNumber" type="text" value="" maxlength="22">
我无法找出字母数字属性缺少什么,因为它没有呈现适当的html属性。我认为,在
字母数字属性
的情况下,您的问题在于没有为字母数字
类型的验证器添加JavaScript适配器
您的代码中肯定有这样的内容:
$.validator.unobtrusive.adapters.add('ssn', function(options) { /*...*/ });
上面的代码声明了SsnAttribute
的客户端适配器。请注意,它的名称为ssn
,与ModelClientValidationRule
的ValidationType
属性中设置的名称相同
要解决AlphaNumericaAttribute的问题,您应该返回ModelClientValidationRegexRule
,因为它已经为您的案例进行了所有必要的设置(即已有适配器regex
)
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,AllowMultiple=false)]
公共类AlphaNumericaAttribute:RegularExpressionAttribute,IClientValidable
{
公共字母数字属性()
:基底(“^[-A-Za-z0-9]+$”)
{
}
公共IEnumerable GetClientValidationRules(ModelMetadata元数据、ControllerContext上下文)
{
返回新的ModelClientValidationRegexRule(FormatErrorMessage(metadata.GetDisplayName()),模式);
}
}
但是,如果在regex验证后面的客户端应该有额外的逻辑,那么您应该编写并注册自己的不引人注目的适配器
要获得更大的形象并更好地了解如何在ASP.NET MVC中实现自定义验证,您可以参考Brad Wilson的博客文章,请参阅
不寻常验证器的自定义适配器部分。这里是另一种方法,它改编自以下文章:
通过使用,您可以利用已存在的正则表达式的集成来创建自己的继承类型。在视图中如何/在何处生成相应的输入字段?您是否正在为
使用Html.textbox?或者Html.EditorFor
?此调用是否在Html.BeginForm
中?“你能展示一下你的观点吗?”达林。我已经更新了我的问题,以便从视图中显示代码。我正在将TextBoxFor与功能性SSN验证器和非功能性字母数字验证器一起使用。我实际上没有按照描述为SsnAttribute编写JavaScript,因为它会自动正确地呈现和验证。但是,建议使用ModelClientValidationRegexRule()的修复方法确实解决了我的问题!
$.validator.unobtrusive.adapters.add('ssn', function(options) { /*...*/ });
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute : RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRegexRule(FormatErrorMessage(metadata.GetDisplayName()), Pattern);
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute: RegularExpressionAttribute
{
private const string pattern = "^[-A-Za-z0-9]+$";
public AlphaNumericAttribute() : base(pattern)
{
// necessary to enable client side validation
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(AlphaNumericAttribute),
typeof(RegularExpressionAttributeAdapter));
}
}