Range Validation in Web API

Rama Sagar
Posted by in ASP.NET Web API category on for Beginner level | Points: 250 | Views : 11719 red flag
Rating: 4 out of 5  
 1 vote(s)

The RangeValidation is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters.


In this article we will see how to use Range Validation in ASP.NET Application that uses web API..This article is for 
obsolete beginners.

  • Step 1  Create a New Project  Open visual studio 2013 -> click on File -> New Project -> Create new ASP.NET MVC  Web Application -> Name it as SampleRangeValidation,

   Select WEB API Template and click OK

  • Step 2  Now find the Models folder in solution created and right click on it and add a class named Testclient as shown below


Using the code

Here we have used  System.ComponentModel.DataAnnotations namespace

Here we have given user to enter only in range between 1 to 100
using System.ComponentModel.DataAnnotations;

namespace SampleRangeValidation.Models
    public class Testclient

        [Required(ErrorMessage = "Pleasee enter Total Students")]
        [Range(0, 100, ErrorMessage = "Enter number between 0 to 100")]
        public int TotalStudents { get; set; }

  • Step 3  Now Lets modify the Homecontroller  which we can find in Controllers folder..add the following implementation
using SampleRangeValidation.Models;
using System.Web.Mvc;

namespace SampleRangeValidation.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            ViewBag.Title = "Home Page";

            return View();
        public ActionResult Index(Testclient objclient)
            if (ModelState.IsValid)
                ViewBag.message = "Now The number is in Range";
            return View();
  • Step 4  Now Lets modify the View which we find under Views-->Home-->Index.cshtml
@model SampleRangeValidation.Models.Testclient
    ViewBag.Title = "Index";
@using (Html.BeginForm("Index", "Home"))
            <h1>Add Total students</h1>

        @Html.TextBoxFor(m => m.TotalStudents)
        @Html.ValidationMessageFor(m => m.TotalStudents)
            <input type="submit" value="Submit" />

Debug the Application by pressing F5 we should find the following output

 After user input we can see the following validation message


In this article we have seen how to use Range validation.Hope you have enjoyed the article
Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Posted by: Pari098 on: 7/30/2019 | Points: 25
There is lots of upgraded features have been seen in the latest windows http://gethelpwindows10.com and if you get full informative information than you must be seen from this way it is the best zone to know about this windows function.

Login to post response

Comment using Facebook(Author doesn't get notification)