Reputation: 13
I'm trying to validate the value of an input, but using this code I don't get the message (the input box becomes still red):
document.forms.richiediAPE.your-email.addEventListener('blur', function(e) {
if (!e.target.validity.valid) {
e.target.setCustomValidity("Heyy");
}
});
<!-- html -->
<form name="richiediAPE" action="mail.php">
<p>
<label>Name & Surname</label>
<input class="name&surname" type="text" value="" name="your-name" maxlength="3" required/>
</p>
<p>
<label>E-mail</label>
<input class="email" type="email" value="" name="your-email" required/>
</p>
<input class="submit" type="submit" value="Request">
</form>
Upvotes: 1
Views: 668
Reputation:
To get a form in Javascript is with another way:
document.forms['form_name']
document.forms['form_name']['form_element_name']
Using:
document.forms['richiediAPE']['your-email'].addEventListener('blur',function(e){
/* Yourself actions. */
alert(document.forms['richiediAPE']['your-email'].value);
/* This alerts the value of the text input. */
);
But you have to know that in document.forms you get a form and a element by its name (name tag, name=""), sure?
Upvotes: 1