-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailValidation.js
48 lines (37 loc) · 2.18 KB
/
emailValidation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
**Problem**
Implement a function that checks whether an email address is valid.
An email address has two parts: A "local part" and a "domain part."
An @ sign separates the two parts: local-part@domain-part.
The local part is the name of the mailbox; this is usually a username.
The domain part is the domain name (e.g., gmail.com, yahoo.com.ph, or myCompanyName.com).
The domain name contains a server name (sometimes called the mail server name) and a top-level domain (.com, .ph, etc.).
For this practice problem, use the following criteria to determine whether an email address is valid:
- There must be one @ sign.
- The local part must contain one or more letters (A-Z, a-z) and/or digits (0-9).
It may not contain any other characters.
- The domain part must contain two or more components with a single dot (.) between each component.
Each component must contain one or more letters (A-Z, a-z) only.
To keep things simple, you don't need to check whether the domain part or top-level domain is "real" or "official".
Note: don't use this criteria for real email validation logic in your programs.
We are using greatly simplified criteria to let you concentrate on the fundamentals of JavaScript,
and not on the specifics of email addresses.
**Examples / Test Cases**
**Data Structures**
**Algorithm**
1. use regex
*/
function isValidEmail(email) {
return !!email.match(/^[a-z0-9]+@[a-z]+(\.[a-z]+)+$/i);
}
console.log(isValidEmail('[email protected]')); // returns true
console.log(isValidEmail('[email protected]')); // returns true
console.log(isValidEmail('[email protected]')); // returns true
console.log(isValidEmail('[email protected]')); // returns true
console.log(isValidEmail('HELLO123@baz')); // returns false
console.log(isValidEmail('[email protected]')); // returns false
console.log(isValidEmail('foo@baz.')); // returns false
console.log(isValidEmail('foo_bat@baz')); // returns false
console.log(isValidEmail('[email protected]')); // returns false
console.log(isValidEmail('[email protected]')); // returns false
console.log(isValidEmail('[email protected]')); // returns false