This actually has nothing to do with the fact you are using Ajax.BeginForm(). It would happen if you were using Html.BeginForm() as well. It’s happening because MVC doesn’t know that your EmailAttribute should be validated as a RegularExpressionAttrbiute.
You have two options:
<div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y:
hidden;" id="_mcePaste">One, just use the RegularExpressionAttribute on your model instead of your custom EmailAttribute:</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div>
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Or, teach your EmailAttribute that it should be validated using the same adapter as the RegularExpressionAttribute uses:</div>
<div></div><div>One, just use the RegularExpressionAttribute on your model instead of your custom EmailAttribute:</div><div>
</div><div>Or, teach your EmailAttribute that it should be validated using the same adapter as the RegularExpressionAttribute uses:</div></div><div>
public class EmailAttribute : RegularExpressionAttribute
{
public EmailAttribute()
: base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$") { }
static EmailAttribute()
{
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));
}
}
</div><div>Edit:</div><div>I have no idea why the forums is blocking the regular expression in the first code block, but you likely
know what supposed to go there. :) I think you should just use the second option anyhow. </div>
Nick Riggs
Member
502 Points
81 Posts
Re: AJAX.BeginForm and Html.ValidationSummary
Mar 23, 2010 03:13 PM|LINK
This actually has nothing to do with the fact you are using Ajax.BeginForm(). It would happen if you were using Html.BeginForm() as well. It’s happening because MVC doesn’t know that your EmailAttribute should be validated as a RegularExpressionAttrbiute. You have two options:
<div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">One, just use the RegularExpressionAttribute on your model instead of your custom EmailAttribute:</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Or, teach your EmailAttribute that it should be validated using the same adapter as the RegularExpressionAttribute uses:</div> <div></div><div>One, just use the RegularExpressionAttribute on your model instead of your custom EmailAttribute:</div><div>[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Invalid email format.")] public string EmailAddress { get; set; }</div><div>Or, teach your EmailAttribute that it should be validated using the same adapter as the RegularExpressionAttribute uses:</div></div><div>public class EmailAttribute : RegularExpressionAttribute { public EmailAttribute() : base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$") { } static EmailAttribute() { DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter)); } }</div><div>Edit:</div><div>I have no idea why the forums is blocking the regular expression in the first code block, but you likely know what supposed to go there. :) I think you should just use the second option anyhow. </div>