[해결로 표시] 이메일 유효성 검사 - JavaScript 작업

  

3
주제 스타터

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.

예시:

입력:
valid@email.bg

출력:
Valid


입력:
invalid@emai1.bg

출력:
Invalid


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

1개 답글
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");
공유: