Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function validateEmail(email) {
- // Regular expression to match a valid email address
- const regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
- // If the email address does not match the regular expression, return false
- if (!regex.test(email)) {
- return false;
- }
- // Otherwise, the email address is valid, so return true
- return true;
- }
- // On change of the email input field, call the validateEmail() function
- document.getElementById("email").addEventListener("change", function() {
- const email = document.getElementById("email").value;
- const isValid = validateEmail(email);
- // If the email address is invalid, display an error message
- if (!isValid) {
- document.getElementById("emailError").innerHTML = "Invalid email address";
- } else {
- document.getElementById("emailError").innerHTML = "";
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement