ASP MVC 5 Client Validation for Range of Datetimes

后端 未结 5 1639
旧巷少年郎
旧巷少年郎 2020-12-06 12:44

I want to check an Datetime field in a form. The field is valid between 01/10/2008 and 01/12/2008. Here is how I defined the viewmodel property:

    [Require         


        
5条回答
  •  执念已碎
    2020-12-06 13:35

    I think you can implement this using custom validation in MVC. Try using this:

    [ValidateDateRange]
    public DateTime StartWork { get; set; }
    

    Here is your custom validation implementation:

    namespace MVCApplication
        {   
    
            public class ValidateDateRange: ValidationAttribute
            {
                protected override ValidationResult IsValid(object value, ValidationContext validationContext)
                {                 
                   // your validation logic
                    if (value >= Convert.ToDateTime("01/10/2008") && value <= Convert.ToDateTime("01/12/2008") )
                    {
                        return ValidationResult.Success;
                    }
                    else
                    {
                        return new ValidationResult("Date is not in given range.");
                    }
                }
            }
        }
    

    UPDATE:

    You can also pass date ranges as parameters to make the validation a generic one:

    [ValidateDateRange(FirstDate = Convert.ToDateTime("01/10/2008"), SecondDate = Convert.ToDateTime("01/12/2008"))]
    public DateTime StartWork { get; set; }
    

    Custom Validation:

        namespace MVCApplication
            {   
    
                public class ValidateDateRange: ValidationAttribute
                {
                  public DateTime FirstDate { get; set; }
                  public DateTime SecondDate { get; set; }
    
                    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
                    {                 
                        // your validation logic
                        if (value >= FirstDate && value <= SecondDate)
                        {
                            return ValidationResult.Success;
                        }
                        else
                        {
                            return new ValidationResult("Date is not in given range.");
                        }
                    }
                }
            }
    

    UPDATE 2: (For Client Side) A very simple jQuery logic should do the client validation. Check below:

    $(document).ready(function(){
    
      $("#btnSubmit").click(function(){
    
        var dt = $("#StartWork").val();
    
        var d = new Date(dt);
        var firstDate = new Date("2008-01-10");
        var secondDate = new Date("2008-01-12");
    
        if(d>= firstDate && d<= secondDate)
        {
          alert("Success");
        }
        else
        {
          alert("Date is not in given range.");
        }
    
      });
    
    });
    

    Please check this JSFiddle to see the working demo:Date Range Validation

提交回复
热议问题