0

I use the following model (part):

[DataType(DataType.PhoneNumber), Display(Name = "Phone #")]
public string PhoneNo { get; set; }

[DataType(DataType.PhoneNumber), Display(Name = "Fax #")]
public string FaxNo { get; set; }

[DataType(DataType.EmailAddress), Display(Name = "Email")]
public string Email { get; set; }

and View:

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

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

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

but when I try to type trash data (i.e. "blablabla"), Email field validator works, but phone validators are not. Why?

Matze
  • 5,100
  • 6
  • 46
  • 69
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 1
    `[DataType]` is not a validation attribute, and if your claiming you get client side validation for `Email` its because your `EditorFor()` method generates `type="email"` and your getting HTML-5 validation, not `jquery` validation, and certainly not server side validation which must be implemented. What validation do you want for `PhoneNo`? (a regex?)` –  Jun 16 '17 at 11:41
  • @StephenMuecke what sense then in DataType at all? – Oleg Sh Jun 16 '17 at 11:55
  • Its an attribute used by the `EditorFor()` method to determine what type of form control to generate, e.g. `type="email"` or `type="number"` etc. For email validation, you use the `[EmailAddress]` attribute –  Jun 16 '17 at 12:04
  • Which type of Phone number you want to validate.can you provide some examples. user jquery validation – ishan joshi Jun 16 '17 at 12:32

2 Answers2

1

DataType will Not give you any validation and show error messages. This is from MSDN:

The DataTypeAttribute attribute lets you mark fields by using a type that is more specific than the database intrinsic types. For example, a string data field that contains e-mail addresses can be attributed with the EmailAddress type. This information can be accessed by the field templates and modify how the data field is processed.

See this answer for how to do phone number validation.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

You are using the wrong attribute types.

If you inspect the source code for DataTypeAttribute you will realize that it is primarily a base attribute used to create custom and targeted validation attribute.

The DataTypeAttribute in the original question is being used incorrectly. There is no other solution than to use the EmailAddressAttribute and PhoneAttribute as demonstrated below in the following example.

[Phone]
[Display(Name = "Phone #")]
public string PhoneNo { get; set; }

[Phone]
[Display(Name = "Fax #")]
public string FaxNo { get; set; }

[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }

Those attribute are derived from DataTypeAttribute and overrides the IsValid method which is what checks that the value is in fact valid.

Nkosi
  • 235,767
  • 35
  • 427
  • 472