[Solved] Email Validation - JavaScript task

  

3
Topic starter

Write a JS function that validates simple emails. The emails should have a username, which consists only of English alphabet letters and digits, a “@” sign, and a domain name after it. The domain should consist only of 2 strings separated by a single dot. The 2 strings should contain NOTHING but lowercase English alphabet letters.

The input comes as single string argument which is an email.

Examples:

Input:
valid@email.bg

Output:
Valid


Input:
invalid@emai1.bg

Output:
Invalid


The output should be printed on the console. If the given email is valid, print “Valid”, if it is not, print “Invalid”.

1 Answer
2

Here is the js solution. Note the line #2 with the regex pattern to check:

function validateEmail(email) {
    let pattern = /^([a-zA-Z0-9]+)@([a-z]+)\.([a-z]+)$/g;
 
    let result = pattern.test(email);
 
    if (result) {
        console.log("Valid");
    } else {
        console.log("Invalid");
    }
}
 
validateEmail("name@emi.bg");
Share: