Naila
Naila

Reputation: 280

Data annotations not working in ASP.NET Core

I have written the following code in my cs.html file, but it's not showing any error if the fields are left empty. Any help please?

<form method="post" action="/Demo/RegisterInput">
    Email:
    <input type="email" data-val="true"
           data-val-email="The Email Address field is not a valid e-mail address."
           data-val-required="The Email Address field is required."
           id="Email" name="Email" value="" /> <br>
    Password:
    <input type="password" data-val="true"
           data-val-required="The Password field is required."
           id="Password" name="Password" /><br>
    <button type="submit">Register</button>
  <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>" />
</form>

And following is my model:

using System.ComponentModel.DataAnnotations;

namespace FormsTagHelper.ViewModels
{
    public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email Address")]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

Upvotes: 0

Views: 2780

Answers (1)

regnauld
regnauld

Reputation: 4336

Missing the validation span below each input. For example:

<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>

Upvotes: 1

Related Questions