Caverman
Caverman

Reputation: 3707

DisplayFormat DataAnnotation not working?

I'm using MVC 5 and I'm trying to format a phone number. I found some examples but I can't seem to get any of them to work. Anyone see what I'm doing wrong?

View:

<div class="form-group">
    @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
    </div>
</div>

ViewModel:

[DisplayName("Phone Number")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###-###-####}")]
public string PhoneNumber { get; set; }

as well as I tried this...although I don't need this as a required field. I'm not sure if the ReqularExpression will make it required?

[DisplayName("Phone Number")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Phone number")]
public string PhoneNumber { get; set; }

UPDATE

Sorry I wasn't clear in my original post but all I'm looking to do is to display a phone number in a ###-###-#### format when I display it in my Edit View using the EditFor helper. It's not a required field and I guess the only validation I really need is the fact that it would be numbers. I'm going to strip out all the dashes when I actually store it in the database.

UPDATE 2

Here is what I currently have in my ViewModel as well as the View. This just shows the phone number without any formatting in my edit. As a side note in case it makes a difference I'm using MVC 5.2.3.

[DisplayName("Phone Number")]
[DisplayFormat(DataFormatString = "{0:###-###-####}", ApplyFormatInEditMode = true)]
public string PhoneNumber { get; set; }

View

<div class="form-group">
    @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
    </div>
</div>

Upvotes: 2

Views: 2683

Answers (1)

Grizzly
Grizzly

Reputation: 5943

It is unclear on exactly what you want here.

This should work for validation purposes.

[DisplayName("Phone Number")]
[RegularExpression(@"[0-9]{3}-[0-9]{3}-[0-9]{4}$", ErrorMessage = "Please enter the phone number as XXX-XXX-XXXX")]
public string PhoneNumber { get; set; }

Also, in your view you need to add ValidationMessageFor:

<div class="form-group">
    @Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
    </div>
</div>

UPDATE:

To get the phone number to display correctly in the Edit View:

[DisplayFormat(DataFormatString = "{0:###-###-####}", ApplyFormatInEditMode = true)]
public string Phone { get; set; }

Upvotes: 1

Related Questions