1

I have a simple Razor page view with the following form fields:

                            <div class="col-sm-6">
                                <label asp-for="Input.FirstName" class="form-label"></label>
                                <input asp-for="Input.FirstName" class="form-control" autocomplete="first-name">
                                <span asp-validation-for="Input.FirstName" class="invalid-feedback" style="display:block"></span>
                            </div>
                            <div class="col-sm-6">
                                <label asp-for="Input.LastName" class="form-label"></label>
                                <input asp-for="Input.LastName" class="form-control" autocomplete="last-name">
                                <span asp-validation-for="Input.LastName" class="invalid-feedback" style="display:block"></span>
                            </div>

and the following model:

            [Required]
            [Display(Name = "First name")]
            public string FirstName { get; set; }

            [Required]
            [Display(Name = "Last Name")]
            public string LastName { get; set; }

Unfortunalely, when I send the form with empty values, despite of the error messages, inputs are still green: enter image description here

I can add required tag to the form, but I would like control it by the jQuery validation, because I am going to add more advanced validations in the future.

Any ideas how to make the field like the following when the data does not met validation requirements?

<input asp-for="Input.FirstName" class="form-control" autocomplete="first-name" required>> enter image description here

1 Answers1

0

a quick answer would be this : [Required(AllowEmptyStrings=false)] but, you can also check this Do not allow empty strings -DataAnnotations your questions is answered here with more info.

JohanEcAv
  • 59
  • 5
  • This is not correct answer. I've added this and the next validation rule like the following `[MinLength(2)]` and the value "A" validation error is "The field First name must be a string or array type with a minimum length of '2'." but the field style is still green – user5492949 Nov 22 '22 at 22:22