1.

What is “Unobtrusive Validation?

Answer»

Unobtrusive javascript library allows to add validation to MVC views without any additional coding, only required to use attributes like RequiredAttribute, RangeAttribute and include the correct script files.

Example:

public class AddUserVM {    [DisplayName("First Name:")]    [Required(ErrorMessage = "Please enter the first name.")]    public string FirstName { get; set; }    [DisplayName("Last Name:")]    [Required(ErrorMessage = "Please enter the last name.")]    public string LastName { get; set; }    [DisplayName("Age:")]    [RANGE(12, 120, ErrorMessage = "You must be between 12 and 120 years of age.")]    public int Age { get; set; }    [DisplayName("Email:")]    [Required(ErrorMessage = "Please enter an email address.")]    [EmailAddress(ErrorMessage = "Please enter a valid email address.")]    public string EmailAddress { get; set; } }

The above class setup is to HANDLE the server side validations. Unobtrusive validations allow us to take existing validation attributes and use them at client-side to make the user experience much better.



Discussion

No Comment Found