13

I have the following properties in my Model

    [Required]
    [DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
    public string PhoneNumber
    {
        get;
        set;

    }

    [Required]
    [DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
    public string EmailAddress
    {
        get;
        set;

    }

The corresponding View is

 <td>
                    Email
                </td>
                <td>
                    @Html.EditorFor(model => model.EmailAddress)
                    @Html.ValidationMessageFor(model => model.EmailAddress, "*")
                </td>
            </tr>
            <tr>
                <td>
                    Phone #
                </td>
                <td>
                    @Html.TextBoxFor(model => model.PhoneNumber)
                    @Html.ValidationMessageFor(model => model.PhoneNumber, "*")
                </td>

When I render this page I see the Required attribute getting triggered. But the DataType attribute is not getting fired if I key in Invalid data.I see the source html and don't see any code being emitted for these validations. I have the following as a part of my view too

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"/>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"/>
kolhapuri
  • 1,581
  • 4
  • 20
  • 31

4 Answers4

16

You could consider using ASP.NET MVC 3 Futures. Here is a nice article describing validations there:

public class UserInformation
{
    [Required]
    public string Name { get; set; }

    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    [Url]
    public string Website { get; set; }

    [Required]
    [CreditCard]
    public string CreditCard { get; set; }

    [Required]
    [FileExtensions(Extensions = "jpg,jpeg")]
    public string Image { get; set; }
}
Alexander Yezutov
  • 3,144
  • 2
  • 21
  • 23
10

See this post:

Is the DataTypeAttribute validation working in MVC2?

It's important to note that the DataType Attribute is usually used for formatting purposes, not for validation. Technically there are a wide range of email formats and phone number formats (see here for email: http://www.regular-expressions.info/email.html).

Also, custom converters can be made to convert seemingly non-email strings into emails (me at domain dot com = me@domain.com), and thus having default validation regexs flies out the window. It is left up to the developer to use the correct regex for their specific purpose, and to ensure they only accept address they believe are accurate.

Community
  • 1
  • 1
cwharris
  • 17,835
  • 4
  • 44
  • 64
10

Related to this question, there are some third party data validation annotations for download at http://dataannotationsextensions.org/

Shan Plourde
  • 8,528
  • 2
  • 29
  • 42
7

I just had a similar issue myself. I had the model setup with a data type of email but it was not being validated as an email. I noticed in the html that the view produced the textbox for the email address had a type of text. I then altered my view as below and this fixed it:

@Html.TextBoxFor(m => m.Email, new { type = "email" })

the was using the jquery validate javascript libary

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Andy Stannard
  • 1,673
  • 2
  • 18
  • 32
  • 1
    The email input type is HTML 5 specific--it just be rendered as a regular text box in older browsers. – Billy Jo Sep 05 '12 at 21:53