1

I have some validation in a view model:

[Required(
    AllowEmptyStrings = false,
    ErrorMessageResourceType = typeof(Resources.ValidationMessages),
    ErrorMessageResourceName = "invalid_cellphoneNumber")]
    [RegularExpression(@"^[1-9]{1}[0-9]{7}$")]
public string CellphoneNumber { get; set; }

And this works, regex and error message. In my markup, I have this:

@Html.TextBoxFor(m => m.CellphoneNumber, new { id = "mobile", type = "tel"})

This will generate an input element. I have also included the right javascripts so that it runs client side as well as server side. The trouble is; when I write something invalid, mvc seems to have created its own error message;

The field CellphoneNumber must match the regular expression '^[1-9]{1}[0-9]{7}$'., instead of using the one I defined. The only way it displays my error message is if the fields are empty and try to submit the form or erase whats write in it, then it changes to my message.

The generated input element looks like this:

<input data-val="true" data-val-regex="The field CellphoneNumber must match the regular expression '^[1-9]{1}[0-9]{7}$'." data-val-regex-pattern="^[1-9]{1}[0-9]{7}$" data-val-required="Cellphone number not valid" id="mobile" name="CellphoneNumber" type="tel" value="" class="input-validation-error">.

Can anyone help?

4

1 回答 1

1

The ErrorMessageResource... properties are per Validation Attribute. I am afraid you have to specify it again for the RegularExpression validation attribute as well.

于 2013-05-16T11:14:55.660 回答