We will go through the model validation in asp.net Core MVC. Simply explained how to validate mobile no, email and also validate the minimum and maximum character of the string.
How to validate registration and login form in asp.net core mvc?
Registration Form
- [Required] - Required field validator.
- [StringLength(15, MinimumLength = 3)] - Name should be minimum 3 character and maximum 15 character.
- [RegularExpression(@"^(\d{10})$", ErrorMessage = "Mobile no not valid")] - Mobile number should be numarical.
- [EmailAddress] - Email validation
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; namespace Dbfirstapp.Models { public class RegistrationViewModel { [Required] [StringLength(15, MinimumLength = 3)] //[StringLength(15, ErrorMessage = "Name length can't be more than 15.")] public string Name { get; set; } [Required] [RegularExpression(@"^(\d{10})$", ErrorMessage = "Mobile no not valid")] public string Mobile { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] [StringLength(10, MinimumLength = 6)] public string Password { get; set; } [Required] [NotMapped] // Does not effect with your database [Compare("Password")] public string ConfirmPassword { get; set; } } }
Registration UI asp.net core mvc
@model RegistrationViewModel @{ ViewData["Title"] = "Register"; } <h2 class="text-center">Registar</h2> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="jumbotron"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <form role="form" asp-action="Registar" asp-controller="Account" method="post"> <div class="form-group"> <label for="Name">User Name</label> <input type="text" name="Name" asp-for="Name" class="form-control input-sm" autocomplete="off" placeholder="Name"> <span asp-validation-for="Name" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Mobile"></label> <input type="text" name="Mobile" autocomplete="off" asp-for="Mobile" class="form-control input-sm" placeholder="Mobile"> <span asp-validation-for="Mobile" class="text-danger"></span> </div> <div class="form-group"> <label for="Email">Email</label> <input type="email" name="Email" asp-for="Email" autocomplete="off" class="form-control input-sm" placeholder="Email Address"> <span asp-validation-for="Email" class="text-danger"></span> </div> <div class="form-group"> <label for="Password">Password</label> <input type="password" name="Password" asp-for="Password" autocomplete="off" class="form-control input-sm" placeholder="Password"> <span asp-validation-for="Password" class="text-danger"></span> </div> <div class="form-group"> <label for="ConfirmPassword">Password</label> <input type="password" name="ConfirmPassword" asp-for="ConfirmPassword" autocomplete="off" class="form-control input-sm" placeholder="ConfirmPassword"> <span asp-validation-for="ConfirmPassword" class="text-danger"></span> </div> <input type="submit" value="Register" class="btn btn-info btn-block"> <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>"> </form> </div> </div> </div>
- for="Name" and asp-for="Name" both are linked from label to textbox diplaying error field while validating.
- <span asp-validation-for="ConfirmPassword" class="text-danger"></span> - If validation faild span control appeared below of the textbox.
- class="text-danger" - This is bootstrap class for display the error message format.
Each and every control bottom span tag using for displaying a validation error message.
<span asp-validation-for="Email" class="text-danger"></span>
No comments:
Post a Comment