Tuesday 8 October 2013

MVC Tips : Range Validation using Data Annotations in ASP.Net MVC framework

We can do Range validation using annotations in MVC for that we are using 'Range' attribute. In the Range attribute we could specifies minimum and maximum value for a numerical value.

The first parameter to the attribute is the minimum value, and the second parameter is the maximum value. The values are inclusive. The Range attribute can work with integers, doubles, dates, etc. We can specify Type in overloaded version constructor.

Here is example for this.

In this example we take one 'Fee' field. Which accepts numeric integer value between 100 and 500.
If we enter value outside this range it will raise validation message.

Here is Code for this. Model (Customer.cs): 

    public class Customer
    {
        public int CustomerID { get; set; }

        [Required]
        [StringLength(20,MinimumLength=2)]
        public string FirstName { get; set; }

        [StringLength(10)]
        public string LastName { get; set; }

        [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
        ErrorMessage="Email Address is not in proper format.")]
        public string Email { get; set; }

        [Required]
        [Range(100,500,
        ErrorMessage="Please Enter Integer Value Between 100 and 500.")]
        public int Fee { get; set; }
    

    }

View :
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>Customer</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName,"", new { style="color:red"})
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.LastName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName,"", new { style="color:red"})
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email,"", new { style="color:red"})
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Fee)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Fee)
                @Html.ValidationMessageFor(model => model.Fee,"", new { style="color:red"})
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }

Output :


Below are the books that you would like :

No comments:

Post a Comment