Reputation: 2307
May be this can be a question some more similar to others But I haven't find the solution that I want from those.
I want to show the Validation message I have set for particular field in MVC::
My ViewModel is::
public class MainDocumentViewModel
{
[Required(ErrorMessage = "Please Enter Valid Customer Name")]
public long CustomerID { get; set; }
}
My View is as ::
<form>
@Html.HiddenFor(x => x.CustomerID)
@Html.ValidationMessageFor(x=>x.CustomerID)
<button type="submit">Submit</button>
</form>
But after submitting the form, I am not getting any error. Help me on this. Thanks in advance.
Upvotes: 3
Views: 8822
Reputation: 7525
for validating only for particular hidden field, do this.
<script>
$.validator.setDefaults({
ignore: $('#CustomerID')
});
</script>
OR for specific form
$('#myform').validate({
ignore: []
});
Upvotes: 4
Reputation: 22619
If you are using jQuery validation plugin by default it will ignore hidden
fields. To validate hidden fields change the default settings
$.validator.setDefaults({
ignore: [],
// other default options
});
Upvotes: 2