Reputation: 376
I am validating dates and I checked support for browsers easily, and the validating function works fine on Firefox, cause when I send an invalid date, I try to create a new Date from the value received. If the value of the new date is "Invalid Date" then return false and alert with an error message. The problem is that on chrome, this doesn't work at all. I have my ajax to be executed whenever dates are empty (cause I want to let the user leave date fields empty and still be able to filter data).
However, in Chrome since it is sending the value as empty when I insert an invalid date (for example 2016-02-31) the ajax is executed taking as if the field was empty when it's not. I want to prevent this from happening.
Im checking all html attributes and events that could do this, but I'm not sure what should I use. I tried onerror passing a function that returns false. Nothing changed. onpatternmismatch maybe?
Upvotes: 0
Views: 527
Reputation: 6150
You can detect such invalid date by the following ways:
input.validity.badInput
if you'd like to detect this particular error!input.validity.valid
or !input.checkValidity()
if you'd like to detect min/max/required validity errors as well as invalid date!form.checkValidity()
if you'd like to detect validity errors of all of elements associated to the form.Upvotes: 1