Validation and Data Annotations In MVC 5
Validation is way to check data or information is authentic or not. Here we are discuses How to validate data or information from client Side Step By step
step 1:
use namespace
System.ComponentModel.DataAnnotations;
step2:
Create Model class
Let Us consider Create Model Class employee
and some property define as
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailConfirm { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public decimal Price { get; set; }
step3
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[StringLength(160)]
public string FirstName { get; set; }
[Required]
[StringLength(160)]
public string LastName { get; set; }
[Required]
[StringLength(160, MinimumLength=3)]
public string FirstName { get; set; }
Step 4:
RegularExpression
Some properties of Order require more than a simple presence or length check. For example,
you want to ensure the Email property of an Order contains a valid, working e-mail address.
Unfortunately, ensuring an e-mail address is working without sending a mail message and waiting
for a response is practically impossible. What you can do instead is ensure the value looks like a
working e-mail address using a regular expression:
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
Range
The Range attribute specify minimum and maximum constraints for a numerical value. If the
field only wanted to serve middle-aged customers, you could add an Age property to the
Order class and use the Range attribute as in the following code:
[Range(35,44)]
public int Age { get; set; }
The values are inclusive. The Range attribute can work with integers and doubles, and
another overloaded version of the constructor takes a Type parameter and two strings (which can
allow you to add a range to date and decimal properties, for example).
[Range(typeof(decimal), "0.00", "49.99")]
public decimal Price { get; set; }
Compare:
Compare ensures two properties on a model object have the same value. For example, you might
want to force customers to enter their e-mail address twice to ensure they didn’t make a typographi-
cal error:
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
[Compare("Email")]
public string EmailConfirm { get; set; }
[DataType(DataType.Password)]
[Display(Name="Password")]
public string Password { get; set; }
Remote
The ASP.NET MVC framework adds an additional Remote validation attribute. This attribute is in the System.Web.Mvc namespace.
The Remote attribute enables you to perform client-side validation with a server callback. Take, for example, the UserName property of the RegisterModel class in the MVC Music Store. No two user should have the same UserName value, but validating the value on the client to ensure the value is unique is difi cult (to do so you would have to send every single username from the database to the client). With the Remote attribute you can send the UserName value to the server, and compare the value against the values in the
database.[Remote("CheckUserName","Account")]
database.[Remote("CheckUserName","Account")]
public string UserName { get; set; }
No comments:
Post a Comment