i

ASP.Net A Complete Guide

Validation

Validations mean applying some rules so the user cannot enter the wrong input. In MVC Data annotation is used to do the modal validation. The data annotation is an inherited System.ComponentModel.DataAnnotations assembly. In the HTML view, you do not even need to add a label.

The "ComponentModel.DataAnnotations" assembly has many built-in validation attributes, for example:

  • Required

  • Range,

  • RegularExpression,

  • Compare

  • StringLength

  • Data type

Sr.No

 

 

1

RegularExpression

The value should  match with given Regular Expression

2

MaxLength

Maximum length for text box/text area

3

MinLength

Minimum length for text box/text area

4

Required

The field is required

5

EmailAddress

Validate email address against the predefined format

6

StringLength

Specifies maximum length for text box/text area

7

Range

Specifies maximum and minimum length  for a numeric field

8

Phone

Validate the expression for phone numbers with the given regularExpression

 

 

Ex:

public class Employee

{

    public int EmployeeId { get; set; }

    

    [Required]

    public string Name { get; set; }

      

    [Range(20,60)]

    public int Age { get; set; }

}

 

 

Output: