Check email id validation
Here is a simple email ID validation function in JavaScript:
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;
}
}
// Example usage:
var email = "[email protected]";
if(validateEmail(email)) {
console.log("Email is valid");
} else {
console.log("Email is not valid");
}
This function uses a regular expression to check if the email ID is in the correct format. The regular expression used here is a common one for email validation, but it's not foolproof and may not match all valid email addresses.
Here's a breakdown of how the regular expression works:
^
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 and only contain letters)$
matches the end of the string
If the email ID matches this regular expression, the function returns true
, otherwise it returns false
.