Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC3 Email/Phone Data Annotations don't work

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"/>
like image 318
kolhapuri Avatar asked Apr 12 '11 02:04

kolhapuri


People also ask

What is Data Annotation in MVC c#?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.

Why data annotation is not working in MVC?

Answers. You have added the Assessment type as a parameter in the post action method. Hence MVC try to create a new instance of Assessment class. If the value for Title field is not provided in the view, then it will try to set the value for Title property as null.


2 Answers

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; }
}
like image 155
Alexander Yezutov Avatar answered Oct 27 '22 10:10

Alexander Yezutov


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 = [email protected]), 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.

like image 30
cwharris Avatar answered Oct 27 '22 09:10

cwharris