In my previous article we learned how to do validation in ASP.NET Web from using Validation Control as well as DataAnnotations attributes.
You can read that article here:
Validation In ASP.NET Web Form.
Now we will do the same thing here in ASP.NET MVC Application.
Step 1: Create new ASP.NET Web Application, select MVC template and click 'Ok'.
It will create Model, View and Controller folders in project.
Step 2: Now let's create a Model class first, for that right click on Models folder, click Add, then Class and give a name 'UserRegistration.cs'.
Step 3: Now let us create property for the following fields and implement validation for the field.
For that add the following code in your model class file. Remember these are the same things we have done in 'Class Library' in my
previous article.
- public class UserRegistration
- {
- [Required(ErrorMessage = "Name is required")]
- public string name
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "email is required")]
- [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", ErrorMessage = "Invalid email format.")]
- public string email
- {
- get;
- set;
- } = "";
-
- [Required(ErrorMessage = "Mobile is required")]
- [RegularExpression(@"\d{10}", ErrorMessage = "Please enter 10 digit Mobile No.")]
- public string mobile
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Password is required")]
- [DataType(DataType.Password)]
- public string Password
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Confirm Password is required")]
- [DataType(DataType.Password)]
- [Compare("Password")]
- public string ConfirmPassword
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Age is required")]
- [Range(typeof(int), "18", "40", ErrorMessage = "Age can only be between 18 and 40")]
- public string age
- {
- get;
- set;
- }
-
- }
Step 4: Now, let's add controller, Right click on controllers folder, click Add and then Controller.
Step 5: In your controller there is a default ActionResult 'Index'. Right click on that and click 'Add View':
It will open
Add View dialog box, here select Template 'Create', so it will create view with all the fields.
From the Model Class select the ModelClass '
UserRegistration' which we have created in step 2.
By default, the Create and Edit scaffolds now use the Html.EditorFor helper.
In the Index.cshtml View, it will generate page using razor syntax it has,
- @Html.EditorFor : Bind the Model property an HTML Input element.
- @Html.ValidationMessageFor : It is a html helper that display validation error message if model state is not valid.
- @class = "text-danger" : Apply Validation class.
Step 6: Don't forget to add jqueryval in the footer part of our view.
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
That's it, now run your application and check all validations.
Its works fine for Required fields: Regular Expression for email and mobile, range validation, compare validation, etc.
Hope you like the simple steps to create Validation using data annotation in ASP.NET MVC.