-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
33 lines (29 loc) · 1.05 KB
/
script.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
var userInput = document.getElementById("name");
var avatar = document.getElementById("avatar");
var downloadLink = document.getElementById("imgsrc");
var usrname = document.getElementById("usrname");
async function generateAvatar(userName) {
avatar.style.display = "initial";
var apiUrl = `https://api.dicebear.com/7.x/micah/jpg?seed=${userName}`;
avatar.src = apiUrl;
usrname.textContent = `Name: ${userName}`;
downloadLink.href = apiUrl;
downloadLink.style.display="inherit";
avatar.onerror = () => {
console.clear();
avatar.style.display = "none";
downloadLink.style.display = "none";
gender.textContent = "Sorry, no avatar found 😄";
}
userInput.value="";
}
function invokeFunc() {
var regex = /^([a-zA-Z]+){3,10}$/;
(regex.test(userInput.value) === false) ? alert("please enter a valid name!") : generateAvatar(userInput.value.toLowerCase());
}
function EventForKeyPress(event) {
if (event.keyCode === 13) {
invokeFunc();
}
}
userInput.addEventListener("keypress", EventForKeyPress);