FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

adding more complex custom validation example · jackcoolpython/learning-area@6c9cd03 · GitHub

Repository navigation

Commit 6c9cd03

Browse files
adding more complex custom validation example
1 parent 141bd01 commit 6c9cd03

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

‎html/forms/form-validation/detailed-custom-validation.html‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,37 +88,41 @@
8888
// is valid, we remove the error message.
8989
emailError.innerHTML = ''; // Reset the content of the message
9090
emailError.className = 'error'; // Reset the visual state of the message
91+
} else {
92+
// If there is still an error, show the correct error
93+
showError();
9194
}
9295
});
9396

9497
form.addEventListener('submit', function (event) {
95-
// Each time the user tries to send the data, we check
96-
// if the email field is valid. If it is, we let the form submit
98+
// if the email field is valid, we let the form submit
9799

98-
// If it isn't, we start the process of working out why it isn't
99-
// and display an appropriate error message
100100
if(!email.validity.valid) {
101-
if(email.validity.valueMissing) {
102-
// If the field is empty
103-
// display the following error message.
104-
emailError.textContent = 'You need to enter an e-mail address.';
105-
} else if(email.validity.typeMismatch) {
106-
// If the field doesn't contain an email address
107-
// display the following error message.
108-
emailError.textContent = 'Entered value needs to be an e-mail address.';
109-
} else if(email.validity.tooShort) {
110-
// If the field doesn't contain an email address
111-
// display the following error message.
112-
emailError.textContent = `Email should be at least ${ email.minL ength } characters; you entered ${ email.value.length }.`;
113-
}
114-
115-
// Update the styling appropriately
116-
emailError.className = 'error active';
117-
118-
// Finally we prevent the form from being sent by canceling the event
101+
// If it isn't, we display an appropriate error message
102+
showError();
103+
// Then we prevent the form from being sent by canceling the event
119104
event.preventDefault();
120105
}
121106
});
107+
108+
function showError() {
109+
if(email.validity.valueMissing) {
110+
// If the field is empty
111+
// display the following error message.
112+
emailError.textContent = 'You need to enter an e-mail address.';
113+
} else if(email.validity.typeMismatch) {
114+
// If the field doesn't contain an email address
115+
// display the following error message.
116+
emailError.textContent = 'Entered value needs to be an e-mail address.';
117+
} else if(email.validity.tooShort) {
118+
// If the data is too short
119+
// display the following error message.
120+
emailError.textContent = `Email should be at least ${ email.minLength } characters; you entered ${ email.value.length }.`;
121+
}
122+
123+
// Set the styling appropriately
124+
emailError.className = 'error active';
125+
}
122126
</script>
123127
</body>
124128

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL