How can use validation in mvc ?

Total Post:73

Points:517
C#  Mvc  Mvc3  Mvc2  Mvc4 
 550  View(s)
Ratings:
Rate this:

Server-side validation and clint side validation. 

  1. Post:11

    Points:81
    Re: How can use validation in mvc ?

    Hi friends,

    Today I'm going to describe the use of validation in asp.net MVC. If you have used validation control in ASP.NET, it will be easy for you. So let's start it...

    First of all, we should discuss about to validation. Like as :

    How can use validation in mvc ?

    What is validation?

    That is the process of evaluating software during the development process or at the end of the development process to determine whether it satisfies specified business requirements. The validation testing ensures that the product actually meets the client's needs. This can also be defined as to demonstrate that the product fulfills its intended use when deployed on an appropriate environment.

    How can use validation in mvc ?

    Using the Data Annotation Validator Attributes

    If we use the Data Annotations Model Binder, we use validator attributes to perform validation. where System.ComponentModel.DataAnnotations namespace includes the following validator attributes:

    1.  Range – Enables you to validate whether the value of a property falls between a specified range of values.
    2.  RegularExpression – Enables you to validate whether the value of property matches a specified regular expression pattern.
    3.  Required – Enables you to mark a property as required.
    4.  StringLength – Enables you to specify a maximum length for a string property.
    5.  Validation – The base class for all validator attributes.

     Note : When we need validation are not satisfied by any of the standard validators then you always have the option of creating a custom validator attribute by inheriting a new validator attribute from the base Validation attribute. 

    That is the goal of the web form validation is to ensure that the user provided necessary and properly formatted information needed to successfully complete an operation. Into this article, we will go beyond the validation itself and explore different validation and error feedback techniques, methods and approaches.

    How can use validation in mvc ?

    SERVER-SIDE VALIDATION

    Into the server-side validation, information is being sent to the server and validated using one of the server-side languages. Whether the validation fails, the response is then sent back to the client, the page that contains the web form is refreshed and feedback is shown. That method is secure because it will work even if JavaScript is turned off in the browser and it can’t be easily bypassed by malicious users. Through the other hand, users will have to fill in the information without getting a response until they submit the form. It results in a slow response from the server. 

    How can use validation in mvc ?

    Model.cs

    using System;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;

    namespace MvcApplication6.Models
    {
        public class BookCustomModel
        {
            public int ID { get; set; }

            [DisplayName("Book Name")]
            [Required]
            public string BookName { get; set; }

            [DisplayName("Author Name")]
            [Required]
            public string BookAuthor { get; set; }

            public string BookImage { get; set; }

        }
    }

    create.cshtml

    @model MvcApplication6.Models.BookCustomModel
    

    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Create</title>
        <style>
            .field-validation-error {
                color: red;
            }
        </style>
    </head>
    <body>
        @if (ViewData.ModelState.IsValid)
        {
            if (@ViewBag.Name != null)
            {
            <b>BookName : @ViewBag.BookName<br />
                AuthorName : @ViewBag.AuthorName
            </b>
            }
        }

        @using (Html.BeginForm())
        {

            @Html.ValidationSummary(true)

            <fieldset>
                <legend>Book</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.BookName)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.BookName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.BookName)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.BookAuthor)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.BookAuthor, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.BookAuthor)
                </div>

                @*<div class="editor-label">
                    @Html.LabelFor(model => model.BookImage)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.BookImage)
                    @Html.ValidationMessageFor(model => model.BookImage)
                </div>*@
                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>
        }

        <div>
            @Html.ActionLink("Back to List", "Index")
        </div>
        <script src="~/Scripts/jquery-2.1.0.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>



    </body>
    </html>

    How can use validation in mvc ?

    CLIENT-SIDE VALIDATION

    Here we know that Server-side validation is enough to have a successful and secure form validation. But, for better user experience, however, you might consider using client-side validation. These type of validation are done on the client using script languages such as JavaScript. Through, the using script languages user’s input can be validated as they type. It means a more responsive, visually rich validation. By the client-side validation, the form never gets submitted if validation fails. The validation is being handled in JavaScript methods that you create (or within frameworks/plugins) and users get immediate feedback if validation fails. 

    How can use validation in mvc ?

    By the client-side validation, the form never gets submitted if validation fails. The validation is being handled in JavaScript methods that you create (or within frameworks/plugins) and users get immediate feedback if validation fails.

    The major drawback of client-side validation is that it relies on JavaScript. Whether the users turn JavaScript off, they can easily bypass the validation. It is why validation should always be implemented on both the client and server. Through combining server-side and client-side methods we can get the best of the two: fast response, more secure validation and better user experience.

    Useful resources

    Here are some of the frameworks, plugins, and tutorials that might help you easily implement validation in your forms.

    •  LiveValidation - Its a small, open-source JavaScript validation library. This enables real-time, rich client validation which can be implemented easily. That is the best way to see its capabilities is to check out the example page.
    •  Validanguage - A validation framework that enables rich client-side validation.
    •  JavaScript Form Validation - A JavaScript validation framework for creating inline validation. You can configure it through JavaScript code or by using the XML configuration file.
    •  UniForm - That is an attempt to standardize form markup (XHTML) and CSS. That can help you style error messages, help text, indicators and so on.
    •  jQuery Validation plugin - It is one of the most popular validation plugins. Like expected from jQuery plugin, it enables validation in one line of code, unless we can also customize it. This has only 14 kb approx and is compatible with the version of jQuery 1.3.2.
    •  jForm - Other different types of jQuery plugins that let you implement validation easily. Neither only that it performs validation but also has the support for live tips. This is still in the development phase but is worth checking.
    •  Dynamic JavaScript Form Validation - Provides easy validation and attractive error messages.
    •  jQuery Form Validator - Other numerous jQuery validation plugin that performs live validation. This is fully customizable and supports localization as well.
    •  Form field hints with CSS and JavaScript - The useful tutorial on how to create helpful tips using CSS and JavaScript. Demo and source are included.
    •  PHP Contact Form with jQuery Validation - This tutorial shows how to implement the validation of a contact form using PHP and JavaScript.
    •  jQuery Shows - how to implement both client-side and server-side validation using PHP, MVC, javascript etc.
    •  Adding Form Validation to WordPress Comments using jQuery - An interesting tutorial for all WordPress users.

      Modified On Jan-17-2019 03:56:56 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.