How to validate Hiddenfields in asp.net MVC5

I hope you have read our last article "How to validate fields not in model class in asp.net MVC5" in which we describe how to validate fields that are not in model class. We use same validation concept to validate hiddenfields in asp.net MVC5. If your hiddenfield is field of model class then you have to add simple ValidationMessage control to show validation message, but if your hiddenfield is not a field of model then you must visit above link.

By default jquery.validate.js is ignore hidden fields to validate and that's beneficial to us, but in certain condition we can not directly validate textbox or any control and we must use hiddenfields (like store list of ids, or temp id) and have to validate it. To resolve this problem we share simple trick to validate hiddenfields and display message for it.

please visit our most interested article on asp.net MVC5

Validate Hiddenfields in asp.net MVC5


open jquery.validate.js file find ignore text, you will see below structure.
how-to-validate-hiddenfields-in-aspnet-mvc5
update ignore parameters as below
ignore:":hidden:Not(.IncludeHidden)" // find hidden field by class
ignore:":hidden:Not(#includeHidden)" // find hidden field by id
Example of How to Validate Hiddenfields in MVC5 application
View
@using (Html.BeginForm())
{
    <h2>Validate Hiddenfield in MVC5 </h2>
    <br/>
    <p>www.dotnetspan.com</p>
@Html.Hidden("Name", "", new { data_val = "true", data_val_required = "This is hidden field Validation Call", @class = "IncludeHidden" })
@Html.ValidationMessage("Name")<br/>
@Html.TextBox("FirstName", "", new { data_val = "true", data_val_required = "First Name is required" })
@Html.ValidationMessage("FirstName")<br/>
<input type="submit" name="submit" value="Submit" />    
}
Demo