Sometimes you need to change the surroundings of an invalid element. You can use the invalid and valid (which is non-standard) events to hook into the appropriate time.
Note, that the events will fire at different times. While invalid is triggered only on form submission, valid is triggered immediately when the input becomes valid. You can change this behavior with the revalidate setting.