Skip to content

Instantly share code, notes, and snippets.

@sdoro
Created January 14, 2018 15:36
Show Gist options
  • Save sdoro/597aa301ddba7b0d31eae922a5937f82 to your computer and use it in GitHub Desktop.
Save sdoro/597aa301ddba7b0d31eae922a5937f82 to your computer and use it in GitHub Desktop.
Form validation
<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>
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";
}
}
input{
display: block;
}
p{
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment