Click here to Skip to main content
14,732,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an MVC 4 application using client-side validation. The form I am struggling with has multiple required fields, but Iam getting the same validation message for each field.

The Razor code looks like this:

<section class="input-label-group">
    @Html.Html5TextBoxFor(m => m.Email, InputTypes.InputType.Email, htmlAttributes: new { autofocus = "autofocus", autocomplete = "off"})
    @Html.ValidationMessageFor(m => m.Email)
<section class="input-label-group">
    @Html.Html5TextBoxFor(m => m.FullName, InputTypes.InputType.Text, htmlAttributes: new { autocomplete = "off" })
    @Html.ValidationMessageFor(m => m.FullName)

The model looks like this:

[Display(Name = "Email")]
[Required(ErrorMessage = "Please enter a valid email address")]
[StringLength(128, ErrorMessage = "The email address may not exceed 128 characters")]
public string Email { get; set; }

[Display(Name = "Full Name")]
[Required(ErrorMessage = "Please enter the user's full name")]
[StringLength(255, ErrorMessage = "The user's name may not exceed 255 characters")]
public string FullName { get; set; }

But looking at the response from the server on initial page load (i.e. before any javascript has run), I get the following:

<section class="input-label-group">
    <input autocomplete="off" autofocus="autofocus" data-val="true" data-val-length="The email address may not exceed 128 characters" data-val-length-max="128" data-val-required="Please enter a valid email address" id="Email" name="Email" placeholder="Email" type="email" value="" />
    <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
<section class="input-label-group">
    <input autocomplete="off" data-val="true" data-val-length="The email address may not exceed 128 characters" data-val-length-max="128" data-val-required="Please enter a valid email address" id="FullName" name="FullName" placeholder="Full Name" type="text" value="" />
    <span class="field-validation-valid" data-valmsg-for="FullName" data-valmsg-replace="true"></span>

Notice the data attributes for the second control refer to email not full name. This causes the validation messages in the final form (using jquery.validate.js and jquery.validate.unobtrusive.js) when submitting empty fields to look like this:

Incorrect Validation Message
Updated 26-Apr-20 17:12pm

Everything looks correct and I see no reason why initial page load would render like you showed here.

I am pretty sure that this is some sort of bug/unexpected behavior. Can you try the same thing but without the apostrophe in [Required(ErrorMessage = "Please enter the user's full name")]? Also, are you using compatible/uptodate versions of jquery.validate.js and jquery.validate.unobtrusive.js?
I have the same problem, learning MVC and playing around. Was this ever solved?
CHill60 27-Apr-20 7:19am
Please don't post comments or questions as solutions to other members' posts - use the "Have a Question or Comment?" link instead
I got it working. I did not have the exact same problem because my rendering looked OK. Regardless, I was getting the same error message for two different types of bad data entry. Pare to the solution was to hard code the error messages in the validator files as follows:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            var rule = new ModelClientValidationRule();
            rule.ErrorMessage = "Common password error.";// //FormatErrorMessage(metadata.GetDisplayName());
            //rule.ValidationParameters.Add("errormsg", _errorMessage);
            rule.ValidationParameters.Add("badpasswords", string.Join(",", badPasswords));
            rule.ValidationType = "password";
            yield return rule;

That, and the two validator script files had to be references right next to each other in the view.

I hope this helps someone.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900