I have two date fields called Start Date and End Date.

I am looking for validation rule where I need to give error message if Start Date is less than End Date. I want to do this using Javascript.

Please guide me in this.

  • 1
    Why not using a native SF validation rule or apex controller? Sep 18, 2014 at 14:15
  • 1
    @mast0r is spot on. Unless you have a compelling reason not to, this can very easily handled by a validation rule that is simply Start_Date__c > End_Date__c Sep 18, 2014 at 14:45
  • Is this meant to be part of a Visualforce page or for a regular Salesforce page?
    – Jagular
    Sep 18, 2014 at 14:58
  • A random comment: I guess you wanted to give error if Start Date is greater than End Date?
    – Novarg
    Nov 26, 2014 at 15:25

1 Answer 1


I totally agree with @mast0r and @sfdc_ninja to use a validation rule if possible.

A possible reason not to use SF validation rules could be, that you would have to submit the page and then receive an error message, if the defined criteria is not met. Validation Rule is still best practice, but from the UX perspective optimizable.

If you really want to go with JS I assume you have skills in JS and you use jquery. Otherwise you have to adapt.

The challenge doing it in JS is, that your users might use different locales, so your date as String must be converted into a timestamp. Depending on what locales you have, you need to search for an appropriate convert function for that, e.g.

The strategy would be to bind a handler to either onchange or onkeyup at both of the input fields, convert both strings, do the math and provide feedback to the user.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .