/*
Assignement:
# HTML
Complete the HTML to have semantic and compliant markups.
# PURE JAVASCRIPT
Dynamically add a user to the users list.
1. Highlight the email input when a user enters an invalid email address and display following message: "please enter a valid email address" in red.
2. Use the addUser function to submit the user's data.
3. If the ajax request returns an error, display the error message in red.
4. Display the newly added user in the users list when the request was successful.
# BONUS
- make WCAG compliant
- add some CSS3 properties
*/
// START YOUR CODE HERE
function onInput(e) {
setError("");
if (e === "email") { setEmailErrorClass(false) }
}
function setEmailErrorClass(addOrRemove) {
var element = document.getElementById("email");
const action = addOrRemove ? "add" : "remove"
element.classList[action]("input-error");
}
function setError(msg) {
document.getElementById("error-msg").innerText = msg
}
function validateEmail(email) {
const isValid = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
if (!isValid) { setEmailErrorClass(true) }
return isValid;
}
function parseResponse(resp) {
console.log(resp)
const { success, user, error } = resp;
if (success) {
var ul = document.getElementById("users");
var li = document.createElement("li");
li.appendChild(document.createTextNode(user.username + ', ' + user.email));
ul.appendChild(li);
document.getElementById("username").value = "";
document.getElementById("email").value = ""
setError("")
} else {
setError(error)
}
}
function onAddUser(e) {
const username = document.getElementById("username").value
const email = document.getElementById("email").value
const isValidEmail = validateEmail(email.trim())
if (!username || !email) {
return setError("Either Username or Email is missing")
}
if (!isValidEmail) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.