Input Validation from Model

If you want to improve user experience of your web services, you need to work on easy and friendly input interfaces for users through out.  Input validation being one of them, and validating user input for ASP.Net MVC is simple and intuitive.  You can use below at Model.

  • Required()
    • This makes specific field a required field.
  • MaxLength(max)
    • This specifies max length users can input for the field.
  • MinLength(min)
    • This specifies min length users can input for the field.
  • Range(min, max)
    • This specifies range of length (from min to max) users can input for the field.
  • RegularExpression(pattern)
    • This specified regular expression that users must match for the field.
  • EmailAddress()
    • The field must be email address. you can do same with RegulareExpression(pattern)
  • Url()
    • The field must be url.
  • Compare(other)
    • The field must match other.
  • CustomValidation(type,method)
    • This calls custom validation.

*The Model must make reference to System.ComponentModel and System.ComponentModel.DataAnnotations.

You can refer to http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation too.

 

Leave a Reply