Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
form
- Loading branch information
Alex Mueller
committed
Mar 30, 2020
1 parent
0b139d7
commit 9fa480f
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,33 @@ | |||
<!DOCTYPE html> | |||
<html lang="en"> | |||
<head> | |||
<meta charset="UTF-8"> | |||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |||
<title>Web Form</title> | |||
</head> | |||
<body> | |||
<h1>Contact Me!</h1> | |||
<form id="myform"> | |||
<input type="text" name="name" id="name" placeholder="Name"> | |||
<span id="name-error"></span> | |||
<input type="email" name="email" id="email" placeholder="Email"> | |||
<span id="email-error"></span> | |||
<input type="text" name="subject" id="subject" placeholder="Subject"> | |||
<span id="subject-error"></span> | |||
<textarea name="message" id="message" cols="30" rows="10" form="myform" placeholder="Message"></textarea> | |||
<span id="message-error"></span> | |||
<button type="submit">Submit</button> | |||
</form> | |||
<style> | |||
form { | |||
display: flex; | |||
flex-direction: column; | |||
align-items: flex-start; | |||
} | |||
textarea { | |||
margin: 0; | |||
} | |||
</style> | |||
<script src="js/form.js"></script> | |||
</body> | |||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
let form = document.querySelector('#myform'); | |||
form.addEventListener('submit', submit); | |||
|
|||
let name = document.querySelector('#name'); | |||
let email = document.querySelector('#email'); | |||
let subject = document.querySelector('#subject'); | |||
let message = document.querySelector('#message'); | |||
let inputList = [name, email, subject, message]; | |||
|
|||
|
|||
|
|||
inputList.forEach(input => { | |||
input.addEventListener('blur', findInputError) | |||
}); | |||
|
|||
function findInputError(e) { | |||
let input = e.currentTarget.id | |||
let errorMessage = document.querySelector(`#${input}-error`) | |||
console.log(input) | |||
if (e.currentTarget.value) { | |||
errorMessage.innerHTML = '' | |||
} else { | |||
errorMessage.innerHTML = `${input} must be filled out.` | |||
} | |||
} | |||
|
|||
function validate(e) { | |||
if (name.value && email.value && subject.value && message.value) { | |||
console.log("hooray"); | |||
} else { | |||
inputList.forEach(input => { | |||
let inputName = input.id; | |||
let errorMessage = document.querySelector(`#${inputName}-error`); | |||
if (input.value) { | |||
errorMessage.innerHTML = ''; | |||
} else { | |||
errorMessage.innerHTML = `${inputName} must be filled out.`; | |||
} | |||
}); | |||
e.preventDefault(); | |||
} | |||
} | |||
|
|||
function submit(e) { | |||
const formData = new FormData(this); | |||
validate(e); | |||
fetch('https://formspree.io/moqlodoz', { | |||
method: 'post', | |||
body: formData | |||
}).then((response) => { | |||
return response; | |||
}).then((data) => { | |||
console.log(data); | |||
}); | |||
} |