Javascript email validation w3schools
According to W3Schools, here is a JavaScript email validation function:
function validateEmail(email) {
var emailReg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (emailReg.test(email)) {
return true;
} else {
return false;
}
}
This function uses a regular expression to check if the input string matches the format of an email address. Here's a breakdown of the regular expression:
^
matches the start of the string[a-zA-Z0-9._%+-]+
matches one or more of the following characters: letters (both uppercase and lowercase), numbers, dot (.), hyphen (-), underscore (_), percent (%), plus sign (+)@
matches the @ symbol[a-zA-Z0-9.-]+
matches one or more of the following characters: letters (both uppercase and lowercase), numbers, dot (.), hyphen (-)\.
matches a period (.) character[a-zA-Z]{2,}
matches the domain extension (it must be at least 2 characters long)$
matches the end of the string
You can use this function like this:
var email = "[email protected]";
if (validateEmail(email)) {
alert("Email is valid");
} else {
alert("Email is not valid");
}
Note that this is a basic email validation and may not cover all possible valid email formats. For more advanced email validation, you may want to use a more complex regular expression or a dedicated email validation library.