-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
3 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
let inputs = document.querySelectorAll('.field[required]') | ||
console.log(inputs) | ||
|
||
window.addEventListener('load', inputs[0].focus()) | ||
|
||
inputs.forEach(function(input) { | ||
input.addEventListener('input', function() { | ||
console.log(`user typed ${input.value}`) | ||
//console.log('user typed'+ input) | ||
}) | ||
|
||
input.addEventListener('blur', function(event) { | ||
const element = event.target | ||
element.setCustomValidity('') | ||
let isValid = element.checkValidity() | ||
|
||
if (!isValid) { | ||
element.setCustomValidity(setErrorMsg(element)) | ||
element.classList.add('invalid') | ||
} else { | ||
element.setCustomValidity(setErrorMsg('')) | ||
element.classList.remove('invalid') | ||
} | ||
}) | ||
}) | ||
|
||
const setErrorMsg = element => { | ||
let errorMsg = '' | ||
switch (element.name) { | ||
case 'mane': | ||
errorMsg = 'This field must be your name!' | ||
break | ||
case 'email': | ||
errorMsg = 'We need your email to avoid duplication!' | ||
break | ||
case 'age': | ||
if (element.value < 18) { | ||
errorMsg = 'Sorry, we can not accept your aplication!' | ||
} else if (element.validity.valueMissing) { | ||
errorMsg = 'We need to know your age!' | ||
} | ||
|
||
default: | ||
errorMsg = 'Please verify this field' | ||
break | ||
} | ||
return errorMsg | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ body { | |
.title { | ||
font-family: 'Luckiest Guy', cursive; | ||
} | ||
.field[required].invalid{ | ||
border-color. | ||
} |