0

I have a field in my Razor:

<div class="form-group">
    @Html.LabelFor(m => m.CreateContractStep1.StartDate, new { @class = "control-label col-md-2" })
    <div class="col-md-4">
        <div class="admin-form theme-primary">
            <label for="StartDate" class="field prepend-icon mbn">
                @Html.TextBoxFor(m => m.CreateContractStep1.StartDate, new { id = "StartDate", @class = "gui-input datepicker", placeholder = "From...", data_bind = "value: CreateContractStep1.StartDate" })
                @Html.ValidationMessageFor(m => m.CreateContractStep1.StartDate, string.Empty, new { @class = "text-danger fw400" })
                <label class="field-icon">
                    <i class="fa fa-calendar-o"></i>
                </label>
            </label>
        </div>
    </div>
</div>

Currently it has some validation present so that the field cannot be left blank. However I would like to add some validation. Based on another field called Termination Date which is in the Model.

I have tried this in my Model:

 [Display(Name = "From")]
 [Required]
 [GreaterThan("TerminationDate", true, "Termination date must be greater than or equal to Notification Date")]
 public DateTime? StartDate { get; set; }

But it's not really working as expected. It does not bring back the Validation Message. I am thinking that I may need a Custom Validator Attribute.

Model:

    #region Create Contract Step 1
    public class ContractStep1ViewModel
    {
        public ContractStep1ViewModel()
        {
            // Default named type to contract.
            NamedType = ContractNamedType.Contract;
        }

        [Display(Name = "Name *")]
        [Required]
        [StringLength(250, ErrorMessage = "The {0} has a maximum of {1} characters.")]
        public string Name { get; set; }

        [Display(Name = "Description")]
        [StringLength(1000, ErrorMessage = "The {0} has a maximum of {1} characters.")]
        public string Description { get; set; }

        [Display(Name = "Contract Type *")]
        [Required]
        public ContractType? ContractType { get; set; }

        [Display(Name = "Contract Or Sideletter *")]
        [Required]
        public ContractNamedType NamedType { get; set; }

        [Display(Name = "Currency Used *")]
        [Required]
        public string Currency { get; set; }

        [Display(Name = "From *")]
        [Required]
        [Compare("End Date")]
        [DataType(DataType.DateTime)]
        public DateTime? StartDate { get; set; }

        [Display(Name = "To")]
        [GreaterThan("StartDate", true, "End Date must be greater than or equal to Start Date")]
        public DateTime? EndDate { get; set; }

        [Display(Name = "Signed")]
        public DateTime? SignedDate { get; set; }
    }

 public class TerminateContractViewModel
    {
        [Required]
        public Guid ContractId { get; set; }

        [Required]
        public Guid RowVersion { get; set; }

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

        [Required]
        public DateTime ContractStartDate { get; set; }

        [Display(Name = "Notification Date")]
        [Required]
        [GreaterThan("ContractStartDate", false, "Notification date must be greater than or equal to Contract Start Date")]
        public DateTime NotificationDate { get; set; }

        [Display(Name = "Termination Date")]
        [Required]
        [GreaterThan("NotificationDate", true, "Termination date must be greater than or equal to Notification Date")]
        public DateTime TerminationDate { get; set; }

        [Display(Name = "Post Term Collection End Date")]
        [Required]
        [GreaterThan("TerminationDate", true, "Post term collection date must be greater than or equal to Termination Date")]
        public DateTime PostTermCollectionEndDate { get; set; }

        // search
        public ContractSearchViewModel SearchModel { get; set; }
        // pagination
        public PagingModel PagingInfo { get; set; }
    }
5
  • 1
    What do you mean by "not really working as expected"? (I'd say StartDate should be LessThan("TerminationDate",...) and not GreaterThan) Commented Jul 20, 2018 at 10:59
  • @Rafalon the validation is not working. I cant use Less Than as we are using Custom Validation. But based on the error message its correct: Start Date must be greater than the termination date Commented Jul 20, 2018 at 11:11
  • 1
    Have you enabled @Html.ValidationSummary(true) in page? Commented Jul 20, 2018 at 11:22
  • @Manoz yes: ` @Html.ValidationSummary(true, string.Empty, new { @class = "text-danger contractModelErrorSummary" })` Commented Jul 20, 2018 at 12:29
  • Would it be worth using a custom validator? As that is what is used for the greater than. As I dont to use JQuery as I am using the HTML Helpers in the Razor for the messages etc. Commented Jul 20, 2018 at 12:53

4 Answers 4

2

you can achieve using jquery like this:

    var sdate=$("#CreateContractStep1_StartDate").val();
    var tdate=$("#CreateContractStep1_TerminationDate").val();

    if(new Date(sdate) > new Date(tdate))
    {
      alert('your message');
    }
Sign up to request clarification or add additional context in comments.

1 Comment

*by converting to date object
0

I think the problem relies with control's name. When you do this

 @Html.TextBoxFor(m => m.CreateContractStep1.StartDate, new { id = "StartDate", @class = "gui-input datepicker", placeholder = "From...", data_bind = "value: CreateContractStep1.StartDate" })

It will generate its name as <input name="CreateContractStep1.StartDate"/>

Where in your Model annotations it looks for the control with name StartDate instead.

You can try changing your control to give it the name it looks for

@Html.TextBox("StartDate", new { id = "StartDate", @class = "gui-input datepicker", placeholder = "From...", data_bind = "value: CreateContractStep1.StartDate", value= CreateContractStep1.StartDate })

1 Comment

I have tried that, but its not worked. However, I have have tested this [Display(Name = "To")] [GreaterThan("StartDate", true, "End Date must be greater than or equal to Start Date")] public DateTime? EndDate { get; set; } Which seems to work. But its not picking up the validation for the Termination date. Its not currently in the View but Termination Date is in the same Model.
0

You can do something with that idea with jQuery.

<script>
    $(function () {
        $('#StartDate').change(function () {
            var inputDate = new Date($('#StartDate').val());
            var CheckDate = new Date();//new Date(2018, 11, 24, 10, 33, 30, 0);
            if (CheckDate > inputDate) {
                alert("Too late");
                $('#StartDate').val(null);
            } else {
                alert("Good!");
            }
        })
    })
    </script>

Comments

0

Is this the way you are searching for?

  1. http://www.devcurry.com/2013/08/using-jquery-and-momentjs-in-aspnet-mvc.html

Or simple js code

  1. $("#EndDate").change(function () { var startDate = document.getElementById("StartDate").value; var endDate = document.getElementById("EndDate").value; if ((Date.parse(startDate) >= Date.parse(endDate))) { alert("End date should be greater than Start date"); document.getElementById("EndDate").value = ""; } });

  2. Custom valid: https://www.codeproject.com/Articles/1191967/Custom-Date-validation-of-date-format-and-other-fo

4.search for any calendar date range picker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.