Implementing Validation Cheat Sheet
Implementing Validation Cheat Sheet
Adding Validation
Decorate properties of your model with data annotations. Then, in the controller:
if
(!ModelState.IsValid)
return
View(…);
.input-‐validation-‐error
{
color:
red;
}
.field-‐validation-‐error
{
border:
2px
solid
red;
}
1
Implementing Validation By: Mosh Hamedani
Data Annotations
• [Required]
• [StringLength(255)]
• [Range(1, 10)]
• [Compare(“OtherProperty”)]
• [Phone]
• [EmailAddress]
• [Url]
• [RegularExpression(“…”)]
Custom Validation
2
Implementing Validation By: Mosh Hamedani
Validation Summary
Client-side Validation
@section
scripts
{
@Scripts.Render(“~/bundles/jqueryval”)
}
Anti-forgery Tokens
In the view:
@Html.AntiForgeryToken()
In the controller:
[ValidateAntiForgeryToken]
public
ActionResult
Save()
{
}