-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidateEmailWithAPI.java
More file actions
45 lines (39 loc) · 1.3 KB
/
Copy pathvalidateEmailWithAPI.java
File metadata and controls
45 lines (39 loc) · 1.3 KB
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
// Basic email validation using regex pattern
function validateEmailWithRegex(email) {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
// Advanced email validation using a third-party API
async function validateEmailWithAPI(email) {
const apiKey = 'YOUR_API_KEY'; // Replace with your actual API key
const apiUrl = `https://emailvalidationapi.com/v1?apiKey=${apiKey}&email=${encodeURIComponent(email)}`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error('API request failed');
}
const result = await response.json();
return result.isValid;
} catch (error) {
console.error('Error validating email with API:', error);
return false;
}
}
// Example usage
const email = 'test@example.com';
if (validateEmailWithRegex(email)) {
console.log(`${email} is a valid email address (regex)`);
} else {
console.log(`${email} is not a valid email address (regex)`);
}
validateEmailWithAPI(email)
.then(isValid => {
if (isValid) {
console.log(`${email} is a valid email address (API)`);
} else {
console.log(`${email} is not a valid email address (API)`);
}
})
.catch(error => {
console.error('Error:', error);
});