Data Annotation
Data validation play major role when developing web application. In Asp.net MVC, we can easily implement validation to application by using Data Annotation attribute to model. Data Annotation attribute classes are present in System.ComponentModel. DataAnnotations namespace. It is a common validation attributes which extend the built-in ASP.NET DataAnnotations (Required, Range, RegularExpression, StringLength etc.,). ASP.NET MVC uses jquery unobtrusive validation to perform client side validation.
MODEL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System.ComponentModel.DataAnnotations; namespace validation.Models { public class ValidationModel { [fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][Required] [Display(Name = "User name")] [StringLength(20,ErrorMessage = "Name not be exceed 20 char")] public string Name { get; set; } [Required(ErrorMessage = "Email_id must not be empty")] [RegularExpression(".+\@.+\..+", ErrorMessage = "Please Enter valid mail id")] public string Email { get; set; } [Required(ErrorMessage = "Please enter phone number")] [RegularExpression(@"^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Enter valid number")] public string Phone { get; set; } [Required(ErrorMessage = "Please enter your address")] public string Address { get; set; } [Range(0,120,ErrorMessage = "Please enter between 0 to 120")] public string Age { get; set; } } } |
View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
@model validation.Models.ValidationModel @{ ViewBag.Title = "Index"; } <h2>Index</h2> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } @using (Html.BeginForm()) { @Html.ValidationSummary(false) <table> <tr> <td> Your Name: @Html.TextBoxFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </td> </tr> <tr> <td> Your Age: @Html.TextBoxFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age) </td></tr> <tr> <td> Your Email: @Html.TextBoxFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) </td></tr> <tr><td> Your Phone: @Html.TextBoxFor(model => model.Phone) @Html.ValidationMessageFor(model => model.Phone) </td></tr> <tr><td> Your Address: @Html.TextBoxFor(model => model.Address) @Html.ValidationMessageFor(model => model.Address) </td></tr> <tr><td> <input type="submit" value="Register Me" /> </td></tr> </table> } |
OUTPUT:
Below output shows the Email validation and Range validation
NOTE :
Validation for password comparsion
1 2 3 |
[DataType(DataType.Password)] [Display(Name = "Confirm new password")] [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] |
Thank you in 2018!
nice site!
Thanks Avadhesh
All the things are clear in one place. Thanks
Thanks for your comments. Soon I will come up with my next post.
Thanks Dhana
Great work, keep it up…
Thanks very interesting blog!
thanks for sharing. awesome.
WONDERFUL Post.thanks for share..extra wait .. …
This site just made my week! I were seeking close to for info on this. I’m glad now that I ran across this webpage. Woohoo!
This is Awesome! Thank you so much.
Pretty excellent post. I just stumbled upon your blog and wanted to say that I have very enjoyed reading your blog posts. Any way I’ll be subscribing for your feed and I hope you write-up again soon.