开发者

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

I have t开发者_StackOverflow中文版he 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"/>


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; }
}


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜