The setup:
- ASP.NET MVC 4 with client-side validation (+ unobtrusive javascript) enabled.
- jquery 1.8.2
- jquery.validate 1.10.0
- jquery.validate.unobtrusive
The html:
<form action="/" method="post">
<select data-val="true" data-val-required="DropDown is required." id="DropDown" name="DropDown">
<option value="">Select a letter</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<span data-valmsg-for="DropDown" data-valmsg-replace="true"></span>
<br/>
<button type="submit">Submit</button>
</form>
You can see it in action here.
The server-side code for the drop-down is pretty uninteresting:
// View Model
public class ViewModel
{
[Required]
public string DropDown { get; set; }
}
// View
@Html.DropDownListFor(m => m.DropDown,
new SelectList(new[] { "A", "B", "C" }),
"Select a letter")
And here are the steps to see the issue:
- With the mouse, select one of the values (A, B, C).
- With the mouse, select the default, empty, value (Select a letter). No required message is displayed.
- With the arrow keys, select one of the values (A, B, C).
- With the arrow keys, select the default, empty, value (Select a letter). This time, the required message is displayed.
- With the mouse, select one of the values (A, B, C). Required message disappears.
- With the mouse, select the default, empty, value (Select a letter). Required message is displayed.
It seems that the events don't kick in for the mouse events until validation is triggered for the first time (either by changing the values with the keyboard or pressing the submit button). Any explanations as to why this is happening (am I doing something wrong?), or workarounds, would be greatly appreciated.
Thanks!