Implement Custom user defined Age Range Validation / Data Annotations rules in MVC 4 application.

MVC is DRY  means don't repeat your code, just write once and get result in every where. One of the most populer of this is "validation on model". In case of validation, we can apply validation on our model and after applying validation we don't need to write code for validation anywhere even we create multiple view /page for CURD operation. There is some built-in validation like Required, StringLength, data type, Range etc. But what if we need some custom validation?.  Don't worry, MVC framework has great extensibility feature and because of this we can create our own customized data annotation attributes. Here I am going to explain How to implement Custom user defined Validation / Data Annotations rules in MVC 4 application. In this article, I will explain how you can create your own custom validation attribute for validation age range and also how to make it work on client side as well.
Custom Age Validation
Implement Custom user defined Age Range Validation / Data Annotations rules in MVC 4 application.
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>


 
Posted By :