Check mail id is valid or not
To check if an email address is valid or not, you can use a regular expression (regex) to validate the format of the email address. Here's an example of a regex pattern that can be used to validate an email address:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
This pattern matches most common email address formats, including:
- Local part (before the
@
symbol): one or more characters (letters, numbers, dot, hyphen, underscore, plus sign, or percent sign) - Domain (after the
@
symbol): one or more characters (letters, numbers, dot, or hyphen) - Top-level domain (after the last dot): two or more characters (letters only)
Here's a breakdown of the pattern:
^
matches the start of the string[a-zA-Z0-9._%+-]+
matches one or more characters that are letters (both uppercase and lowercase), numbers, dot, hyphen, underscore, plus sign, or percent sign@
matches the@
symbol[a-zA-Z0-9.-]+
matches one or more characters that are letters, numbers, dot, or hyphen\.
matches a dot (escaped with a backslash because dot has special meaning in regex)[a-zA-Z]{2,}
matches two or more characters that are letters only (this is the top-level domain)$
matches the end of the string
You can use this pattern in a programming language or a text editor to validate an email address. For example, in JavaScript, you can use the test()
method to validate an email address:
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const email = "[email protected]";
if (emailRegex.test(email)) {
console.log("Email is valid");
} else {
console.log("Email is not valid");
}
Keep in mind that this pattern does not validate whether the email address actually exists or is in use. It only checks the format of the email address.