Validate DateTime in ASP.Net Core MVC

pandeygolu4200
 
on Oct 21, 2022 03:57 AM
673 Views

Hi,

I have one form where i have used jQuery validation (unobtrusive) for client side validation.

After click on submit it is validation properly but when i select date still error message is not hiding.

public class PatientAddParams
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    public string? Gender { get; set; }
    [Required]
    public string BirthDate { get; set; }
    [Required]
    [EmailAddress(ErrorMessage = "Invalid Email")]
    public string Email { get; set; }
    [Required]
    public string Phone { get; set; }
    public string? Phone2 { get; set; }
    [Required]
    public string? SSN { get; set; }
    [Display(Name = "Address")]
    public string Street1 { get; set; }
    public string? Street2 { get; set; }
    [Required]
    public string City { get; set; }
    [Required (ErrorMessage = "Please select State")]
    public string State { get; set; }
    [Required]
    public string Zip { get; set; }
    public string ProviderID { get; set; }
    public string Status { get; set; }
}

 

public IActionResult PatientAdd(PatientAddParams model)
{
    ModelState.Remove("PatientUpdateParams");
    ModelState.Remove("ProviderID");
    ModelState.Remove("Status");
    ModelState.Remove("PrescAdd");
    if (ModelState.IsValid)
    {
        model.ProviderID = HttpContext.Request.Cookies["UserId"] != null ? HttpContext.Request.Cookies["UserId"].ToString() : "";
        model.Status = "Active";
        AccountApiCall accountApiCall = new AccountApiCall(Configuration["ABCAPI"]);                           
        var Response = accountApiCall.PostAPIcall<PatientResponseModel>(model, "patient/add", HttpContext.Request.Cookies["Token"].ToString());
        return Json(Response);
    }
    else
    {
        return View("PatientList");              
    }
}

 

<form method="post" enctype="multipart/form-data" asp-controller="Patient" asp-action="PatientAdd" id="Addfrm">
    <h6 class="modal-title mb-2"><b>Patient Info</b></h6>
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label for="fname">First Name<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.FirstName,new{ @class="form-control",id="fname",@maxlength="50",@minlength="2"})
                <span asp-validation-for="patientadd.FirstName" class="text-danger"></span>                             
            </div>
        </div>
        <div class="col-6">
            <div class="form-group">
                <label for="lname">Last Name<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.LastName,new{ @class="form-control",id="lname",@maxlength="50",@minlength="2"})
                <span asp-validation-for="patientadd.LastName" class="text-danger"></span>                              
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <label for="">Gender<span class="text-danger">*</span></label>
        </div>
        <div class="col-6 ">
            <label class="radio-btn">
                Male
                <input type="radio" name="radio" class="message_pri"  value="M">
                 
                <span class="checkmark"></span>
            </label>
        </div>
        <div class="col-6 ">
            <label class="radio-btn">
                Female
                <input type="radio" name="radio" class="message_pri" value="F">
                <span class="checkmark"></span>
            </label>
        </div>
    </div>
    <div class="form-group">
        <label for="icd-disc">Date Of Birth<span class="text-danger">*</span></label>
 
        <div class="input-group date datepicker-wrapper dateFieldRestrictFutureDate" id="datepicker">
            @Html.TextBoxFor(c => c.patientadd.BirthDate, new { id="dob",@class="form-control"})
            <div class="input-group-addon">
                <span><i class="fa fa-calendar"></i></span>
            </div>
        </div>                   
        <span asp-validation-for="patientadd.BirthDate" class="text-danger"></span>                                    
    </div>
    <div class="form-group">
        <label for="number">Phone Number<span class="text-danger">*</span></label>
        @Html.TextBoxFor(c=>c.patientadd.Phone,new{ @class="form-control",id="phone1",@maxlength="10",@minlength="10"})
                <span asp-validation-for="patientadd.Phone" class="text-danger"></span>                     
    </div>
    <div class="row">
        <div class="col-6 ">
            <div class="form-group">
                <label for="address">Email Address<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.Email,new{ @class="form-control",id="email",@maxlength="30"})
                <span asp-validation-for="patientadd.Email" class="text-danger"></span>                            
            </div>
        </div>
        <div class="col-6 ">
            <div class="form-group">
                <label for="ssn">SSN<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.SSN,new{ @class="form-control",id="ssn",@maxlength="9"})
                <span asp-validation-for="patientadd.SSN" class="text-danger"></span>                             
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label for="fname">Address1<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.Street1,new{ @class="form-control",id="Address1",@maxlength="30"})
                <span asp-validation-for="patientadd.Street1" class="text-danger"></span>                            
            </div>
        </div>
        <div class="col-6">
            <div class="form-group">
                <label for="lname">Address2</label>
                @Html.TextBoxFor(c=>c.patientadd.Street2,new{ @class="form-control",id="Address2",@maxlength="30"})                                                            
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label for="fname">State</label>
                <select class="form-control form-select" id="StateID" name="StateID" data-val="true" data-val-required="Please select state">                                  
                </select>                           
                <span class="text-danger" data-valmsg-for="StateID" data-valmsg-replace="true"></span>
            </div>
        </div>
        <div class="col-6">
            <div class="form-group">
                <label for="lname">City</label>
                <select class="form-select" aria-label="Default select example" id="CityID" name="CityID" data-val="true" data-val-required="Please select City">                                   
                </select>
                 <span asp-validation-for="patientadd.City" class="text-danger"></span>
                 <span class="text-danger" data-valmsg-for="CityID" data-valmsg-replace="true"></span>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label for="fname">Zip<span class="text-danger">*</span></label>
                @Html.TextBoxFor(c=>c.patientadd.Zip,new{ @class="form-control",id="zip",@maxlength="6"})
                  <span asp-validation-for="patientadd.Zip" class="text-danger"></span>                             
            </div>
        </div>
        <div class="col-6">
            <div class="form-group">
                <label for="lname">Mobile Number</label>
                @Html.TextBoxFor(c=>c.patientadd.Phone2,new{ @class="form-control",id="phone2",@maxlength="10"})                              
            </div>
        </div>
    </div>
    <div class="modal-btn d-grid">                       
        <input type="submit" id="AddPatient" class="btn mt-3" value="Add">
    </div>
</form>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download