Skip to content

Instantly share code, notes, and snippets.

@bikashthapa01
Created April 18, 2022 04:15
Show Gist options
  • Save bikashthapa01/56c108be1139a624601a5e89e2218bf9 to your computer and use it in GitHub Desktop.
Save bikashthapa01/56c108be1139a624601a5e89e2218bf9 to your computer and use it in GitHub Desktop.
Simple JavaScript Form Validation
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Form Validation</title>
</head>
<body>
<script type="text/javascript">
function validate(){
if(document.userForm.name.value == ""){
alert("Please Provide Your Name");
document.userForm.name.focus();
return false;
}
if(document.userForm.email.value == ""){
alert("Please Provide Your Email");
document.userForm.email.focus();
return false;
}
if(document.userForm.zipcode.value == ""){
alert("Please Provide Your Zip Code");
document.userForm.zipcode.focus();
return false;
}
if(document.userForm.country.value == "-1"){
alert("Please Provide Your Country");
document.userForm.country.focus();
return false;
}
}
</script>
<form method="post" onsubmit="validate();" name="userForm">
<div>
<label>Name</label>
<input type="text" name="name">
</div>
<div>
<label>Email</label>
<input type="text" name="email">
</div>
<div>
<label>Zip Code</label>
<input type="text" name="zipcode">
</div>
<div>
<label>Country</label>
<select name="country">
<option selected value="-1">
Select Country
</option>
<option value="1">
Nepal
</option>
<option value="2">
India
</option>
<option value="3">
USA
</option>
</select>
</div>
<div>
<input type="submit" value="Save">
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment