Skip to content
Advertisement

Compare date values “instantly” on user input

This question might be similar to this other question:

HTML Comparing 2 Dates with Javascript

But I don’t want to use a button, simply make it instant.

I have 2 inputs of type date and I want to compare between them.

JavaScript

Now the condition works perfectly, It compares the 2 dates and shows an error if the 2nd date is less than the first date. But this is using a button to trigger the comparison.

Is it possible to compare the input dates without a button to trigger the comparison? And using the HTMLSelectElement.setCustomValidity() method?

Advertisement

Answer

I think you can use the onchange event to do this. I modified your snippet a little and put in a simple condition for when NOT to validate. You can adjust it to be more sophisticated.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement