Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make validations in Asp.Net MVC

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 280
    Comment on it

    "How to make validations in Model Class using Asp.Net MVC "

        In this article we will learn about validating Model using Data Annotations . Data Annotations provides us Metadata that describes some set of rules, with the help of which we can validate the property in the Model class itself. To use Data Annotations first include "System.ComponentModel.DataAnnotations assembly" to your Model class.

    Some of the Commonly used Data Annotations are as follows:-

    1. [Required]:- This Data Annotation is used over the property whose value cannot be left blank. It is same like RequiredFieldValidator in Asp.Net.
    Example:

    [Required]
     public string firstName { get; set; }
    

    2. [Display(Name = "UserId")]:- This Data Annotation is used to change the Display name of the property.
    Example: If we wan't Email to viewed as UserId, then:

    [Display(Name = "UserId")]
     public string email { get; set; }
    

    3. [EmailAddress(ErrorMessage="Not valid Email")]:- This Data Annotation is used to validate Emails. We can provide an Error message incase if invalid Email supplied.
    Example:

    [EmailAddress(ErrorMessage = "Not valid Email")]
            public string email { get; set; }
    

    4. [Compare("Password")]:- This Data Annotation is used to compare the value of two properties, for example: if we have compare the Password and Compare Password properties then this Data Annotation will be used. It is same like CompareFieldValidator in Asp.Net.
    Example:

    [Required]
    public string password{ get; set; }
    
     [Required]
     [Compare("password")]
    public string confirmPassword{ get; set; }
    

    5. [RegularExpression("", ErrorMessage = "")]:- This Data Annotation is used to provide custom validations, we also provide an Error message for our custom validation.
    Eample: if we wan't to restrict the user to enter only letters we can write the following Data Annotation:

    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
    

    6. [MaxLength(10,ErrorMessage="Upto 10")]:- This Data Annotation is used to restrict the maximum number of characters the user can input in a particular field. We can provide our range and Error Message in this Annotation.
    Example:

    [MaxLength(10,ErrorMessage="Upto 10")]
     public string mobileNumber { get; set; }
    

    7. [MinLength(5, ErrorMessage="Atleast 5")]:- This Data Annotation is used to restrict the minimum number of characters that the user has to input in a particular field. We can provide our range and Error Message in this Annotation.
    Example:

    [MinLength(10,ErrorMessage="Atleast 10")]
     public string mobileNumber { get; set; }
    

    8. [Phone]:- This Data Annotation is used to validate the phone property, i.e to check wether the entered phone is valid or not.
    Example:

    [Phone (ErrorMessage="Enter valid number")]
    public string phone { get; set; }
    

    These were some of the most used Data Annotations, for complete list you can visit the following link:

    Data Annotations

    Note=> Include validate.js in your project to show the validations client side.

    Hope it helps..... Happy Coding !

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: