I am trying to create one of those standard password forms where you enter a new password once and then a second time for confirmation. I would like that after blurring these fields, if they do not match, both will be marked as invalid, as in the following scenario:
- The user enters the password
abc in #newpassword1 . - User Tabs <<22>.
- The user enters the password
def in #newpassword2 . - There are no user tabs.
- The check detects a mismatch and marks both
#newpassword1 and #newpassword2 as invalid.
I know that I can mark the event target as invalid using e.target.setCustomValidity(...) , but I am not very well versed in the JavaScript event model and cannot understand how to mark another element as invalid based on the target native invalidation .
This is the corresponding snippet of (non-working) code that I am trying to use:
if ( $('#newpassword1').val() != $('#newpassword2').val() ) { errorMessage = "The new passwords you've entered don't match."; $('#newpassword1, #newpassword2').setCustomValidity(errorMessage); }
It seems like this should work, intuitively, but of course not. The error is simply TypeError: $(...).setCustomValidity is not a function .
Please note: I am not asking how to add a red ring or something to the field, I want it to be actually invalid (as in, it has the validity.valid property return false ).
Can this be done?
Thanks!
source share