DataType.EmailAddress not being Validatedhttp://forums.asp.net/t/1622144.aspx/1?DataType+EmailAddress+not+being+ValidatedSun, 12 Jun 2011 08:32:49 -040016221444163571http://forums.asp.net/p/1622144/4163571.aspx/1?DataType+EmailAddress+not+being+ValidatedDataType.EmailAddress not being Validated <p>Hi,</p> <p>I'm not sure if this problem is isolated in MVC2 or if it's isolated with the DataType.EmailAddress, but that's what I'm working on.</p> <p>Anyway, I noticed that no validation is performed at all on my field marked with [DataType(DataType.EmailAddress)], both on client and server.</p> <p>Here is the Model:</p> <p><br> </p> <pre class="prettyprint">public class EmailModel { [Required] [DataType(DataType.EmailAddress)] public string Email {get; set;} [Required] public string Message {get; set;} }</pre><p><br>Here is the View:</p><p><br></p><pre class="prettyprint">&lt;%= Html.ScriptInclude("jquery-1.4.3.min.js") %&gt; &lt;%= Html.ScriptInclude("MicrosoftAjax.js")%&gt; &lt;%= Html.ScriptInclude("MicrosoftMvcValidation.js") %&gt; &lt;% Html.EnableClientValidation(); %&gt; &lt;% using (Html.BeginForm()) {%&gt; &lt;!-- Email Address --&gt; &lt;%: Html.LabelFor(model =&gt; model.Email) %&gt; &lt;%: Html.TextBoxFor(model =&gt; model.Email)%&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Email) %&gt; &lt;!-- Message --&gt; &lt;%: Html.LabelFor(model =&gt; model.Message) %&gt; &lt;%: Html.TextAreaFor(model =&gt; model.Message) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.Message) %&gt; &lt;% } %&gt;</pre><p><br>Here are the Controller Actions:</p><p><br></p><pre class="prettyprint">public ActionResult SendEmail() { return View(new EmailModel()); } [HttpPost] public ActionResult SendEmail(EmailModel model) { if(ModelState.IsValid) { SendEmail(model); } return View(model); }</pre> <p><br> Validation for the [Required] attributes are working fine. It's the DataType.EmailAddress that never gets checked at all. My ModelState.IsValid returns true even if I enter &quot;x&quot; as my email address. Haven't tried the other DataTypes, so I dunno if this is just for the email or for all types.</p> <p>Any ideas?</p> <p>Regards,</p> <p>Tom</p> <p><br> </p> <p><br> </p> <p><br> </p> 2010-11-10T23:00:35-05:004163611http://forums.asp.net/p/1622144/4163611.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Can you post the rendered validation init script (window.mvcClientValidationMetadata stuff)?</p> 2010-11-10T23:57:31-05:004163623http://forums.asp.net/p/1622144/4163623.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Hi,</p> <p>Here it is. At a glance, I don't see the email validation at all.</p> <p><br> </p> <pre class="prettyprint">&lt;script type=&quot;text/javascript&quot;&gt; //&lt;![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({&quot;Fields&quot;:[{&quot;FieldName&quot;:&quot;From&quot;,&quot;ReplaceValidationMessageContents&quot;:true,&quot;ValidationMessageId&quot;:&quot;From_validationMessage&quot;,&quot;ValidationRules&quot;:[{&quot;ErrorMessage&quot;:&quot;The From field is required.&quot;,&quot;ValidationParameters&quot;:{},&quot;ValidationType&quot;:&quot;required&quot;}]},{&quot;FieldName&quot;:&quot;Subject&quot;,&quot;ReplaceValidationMessageContents&quot;:true,&quot;ValidationMessageId&quot;:&quot;Subject_validationMessage&quot;,&quot;ValidationRules&quot;:[{&quot;ErrorMessage&quot;:&quot;The Subject field is required.&quot;,&quot;ValidationParameters&quot;:{},&quot;ValidationType&quot;:&quot;required&quot;}]},{&quot;FieldName&quot;:&quot;Message&quot;,&quot;ReplaceValidationMessageContents&quot;:true,&quot;ValidationMessageId&quot;:&quot;Message_validationMessage&quot;,&quot;ValidationRules&quot;:[{&quot;ErrorMessage&quot;:&quot;The Message field is required.&quot;,&quot;ValidationParameters&quot;:{},&quot;ValidationType&quot;:&quot;required&quot;}]}],&quot;FormId&quot;:&quot;form0&quot;,&quot;ReplaceValidationSummary&quot;:true,&quot;ValidationSummaryId&quot;:&quot;validationSummary&quot;}); //]]&gt; &lt;/script&gt;</pre> <p><br> <br> </p> <p><br> </p> <p><br> </p> <p><br> </p> <p>PS. I changed the model I pasted here earlier to make it shorter and easier to understand, but the only difference is, From == Email, and I took out Subject. But otherwise, they are identical with their attributes, etc</p> 2010-11-11T00:11:08-05:004163639http://forums.asp.net/p/1622144/4163639.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Could you add a call to ValidateFor for Email (or From). I'm sure ValidationMessageFor does the same thing though but give it a try.</p> <p>I am using DataType.EmailAddress in much the same way and see it output correctly. It's very strange that you are seeing only some of the rules.</p> 2010-11-11T00:23:31-05:004163656http://forums.asp.net/p/1622144/4163656.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Hi,</p> <p>Still nothing.</p> <p>I also tried using &quot;jquery-1.4.3.js&quot; to see if the minimized version was buggy, as well as &quot;jquery-1.4.1.min.js&quot; and &quot;jquery-1.4.1.js&quot;&nbsp;because that's what was originally in the project (i updated the jquery with the latest from their site). Nothing still.</p> <p>Regards,</p> <p>Tom</p> <p><br> </p> 2010-11-11T00:36:46-05:004163657http://forums.asp.net/p/1622144/4163657.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Hi,</p> <p>On a side note, there is a validation summary, so I know that ValidateFor didn't work.</p> <p>And not to mention that it completely passes server validation.</p> <p>Regards,</p> <p>Tom</p> 2010-11-11T00:38:07-05:004163669http://forums.asp.net/p/1622144/4163669.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Hi,</p> <p>I tried changing the DataType to other stuff like currency, imageurl, date, etc... none work. I added a StringLength attribute just to make sure that the model was being validated against my modified copy and that shows up fine. It's really this DataType attribute that simply fails outright.</p> <p>Regards,</p> <p>Tom</p> 2010-11-11T00:51:29-05:004163717http://forums.asp.net/p/1622144/4163717.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p><s>Can you add this to your application_start code.</s></p> <p><br> </p> <pre class="prettyprint">DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DataTypeAttribute), typeof(DataTypeValidator));</pre> <p><br> Edit: actually that's not going to work DataTypeValidator is a custom class of mine. Anyway all the adapters should already be registered.</p> 2010-11-11T01:45:46-05:004163823http://forums.asp.net/p/1622144/4163823.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Hi,</p> <p>Worldspawn, so you're using a custom validator for DataType annotations?</p> <p>Sigh, I've had to resort to RegularExpression instead. Though I do hope that I just missed something with the DataType annotation. Hopefully someone can help.</p> <p>Regards,</p> <p>Tom</p> 2010-11-11T03:38:45-05:004163868http://forums.asp.net/p/1622144/4163868.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p></p> <blockquote><span class="icon-blockquote"></span> <h4>tomataus</h4> Though I do hope that I just missed something with the DataType annotation. Hopefully someone can help.</blockquote> <p></p> <p>No, DataType has no builtin validation<br> </p> <p><br> </p> <p>http://forums.asp.net/p/1560346/3852216.aspx</p> 2010-11-11T04:28:59-05:004163971http://forums.asp.net/p/1622144/4163971.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Its been so long since I did it for the first time.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>imran_ku07</h4> No, DataType has no builtin validation</blockquote> <p></p> <p>Well if that's the case, heres some code, note that I work with jQuery validate so you might need to do even more to get this working on the client:</p> <p><br> </p> <pre class="prettyprint">public class DataTypeValidator : DataAnnotationsModelValidator&lt;DataTypeAttribute&gt; { public DataTypeValidator(ModelMetadata metadata, ControllerContext context, DataTypeAttribute attribute) : base(metadata, context, attribute) { this.message = attribute.ErrorMessage; } string message; public override IEnumerable&lt;ModelValidationResult&gt; Validate(object container) { //TODO: Add email and url regularexpression validations. return base.Validate(container); } public override IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules() { List&lt;ModelClientValidationRule&gt; rules = new List&lt;ModelClientValidationRule&gt;(); ModelClientValidationRule rule; switch (Attribute.DataType) { case DataType.EmailAddress : rule = new ModelClientValidationRule() { ErrorMessage = message, ValidationType = &quot;email&quot; }; rule.ValidationParameters.Add(&quot;email&quot;, &quot;true&quot;); rules.Add(rule); break; case DataType.Url : rule = new ModelClientValidationRule() { ErrorMessage = message, ValidationType = &quot;url&quot; }; rule.ValidationParameters.Add(&quot;url&quot;, &quot;true&quot;); rules.Add(rule); break; } return rules; } }</pre> <p><br> And add that RegisterAdapter line of code from my earlier post to your application_start</p> 2010-11-11T05:43:44-05:004164005http://forums.asp.net/p/1622144/4164005.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p></p> <blockquote><span class="icon-blockquote"></span> <h4>worldspawn[]</h4> Well if that's the case, heres some code</blockquote> <p></p> <p>This code is not buitin to MVC by default.<br> </p> 2010-11-11T06:13:16-05:004456280http://forums.asp.net/p/1622144/4456280.aspx/1?Re+DataType+EmailAddress+not+being+ValidatedRe: DataType.EmailAddress not being Validated <p>Use nuget to install MVC2(or 3) futures project.&nbsp; You can use the EmailAddressAttribute.&nbsp; It's also a good reference for how to extend it (and unobtrusively) if you need to.</p> 2011-06-12T08:32:49-04:00