A Pen by Bre'Ana Deen on CodePen.
Created
January 14, 2018 15:36
-
-
Save sdoro/597aa301ddba7b0d31eae922a5937f82 to your computer and use it in GitHub Desktop.
Form validation
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
<form action="" onsubmit = "return verify()" method="POST"> | |
<input id="username" name="username" type="text">Enter your name</input> | |
<p id="validationMessage" hidden>Number must be between 40-90</p> | |
<p id="nanMessage" hidden>Please enter a numeric value</p> | |
<input id="number" name="number" type="text">Enter a number between 40-90</input> | |
<input type="submit"></input> | |
</form> |
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
var numberInput = document.getElementById("number"); | |
var validationMessage = document.getElementById("validationMessage"); | |
function verify(){ | |
if(numberInput.value < 40 || numberInput.value > 90){ | |
validationMessage.style.display = "block"; | |
return false; | |
} | |
else if(isNaN(numberInput.value)){ | |
nanMessage.style.display = "block"; | |
} | |
else{ | |
validationMessage.style.display = "none"; | |
return true; | |
} | |
} | |
numberInput.onchange = function(){ | |
if(isNaN(numberInput.value)){ | |
nanMessage.style.display = "block"; | |
} | |
else{ | |
nanMessage.style.display = "none"; | |
} | |
} |
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
input{ | |
display: block; | |
} | |
p{ | |
color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment