doglin
doglin

Reputation: 1741

MVC Validation : wrong message displayed

Hi this is the RegularExpression in my model code

    [Required]
    [StringLength(127)]
    [RegularExpression("^[a-zA-Z]+$", ErrorMessage = "Enter only alphabets for First Name")]
    public string FirstName { get; set; } //First Name should only use Alphabets

This is the validation in /home/index.aspx page

<%  using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "AudienceRequest" }))
    { %>
        <%= Html.ValidationSummary(true, "To request tickets, please complete the required (*) fields below and click Submit") %>


        <div>
            <label>First Name
            <%= Html.ValidationMessageFor(x => x.FirstName, "*") %></label>
            <%= Html.TextBoxFor(x => x.FirstName) %>

Right now, the validation part works. If I enter BOB333, it won't submit the form. but the message displayed is wrong. It is still displaying "To request tickets, please complete the required (*) fields below and click Submit", instead of "Enter only alphabets for First Name"

Please advise.

Thanks

Upvotes: 1

Views: 779

Answers (1)

StanK
StanK

Reputation: 4770

You need to change

<%= Html.ValidationSummary(true, "To request tickets, please complete the required (*) fields below and click Submit") %>

to

<%= Html.ValidationSummary(false, "To request tickets, please complete the required (*) fields below and click Submit") %>

The boolean flag is to "exclude property errors" - see MSDN

Upvotes: 3

Related Questions