Skip to content

Instantly share code, notes, and snippets.

@alexdwagner
Created March 29, 2024 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdwagner/333846f18750628fe73783d2cf4e7927 to your computer and use it in GitHub Desktop.
Save alexdwagner/333846f18750628fe73783d2cf4e7927 to your computer and use it in GitHub Desktop.
Form Validation - Pramp - 03292024
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign up form</title>
<style>
input {
color: blue
}
</style>
</head>
<body>
<div class="sign-up-form">
<form name="myForm" onsubmit="return performAllChecks()" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" required>
<br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" required>
<br><br>
<label for="email">Email address:</label>
<input type="email" id="email" name="email" required>
<br><br>
<label for="password1">Password:</label>
<input type="text" id="password1" name="password1" required>
<br><br>
<label for="password2">Confirm password:</label>
<input type="text" id="password2" name="password2" required>
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<script>
function performAllChecks(){
return validateForm() && passwordIsString() && passwordsMatch();
}
function validateForm(){
let fname = document.forms["myForm"]["fname"].value
let lname = document.forms["myForm"]["lname"].value
let email = document.forms["myForm"]["email"].value
let password1 = document.forms["myForm"]["password1"].value
let password2 = document.forms["myForm"]["password2"].value
if (!fname || !lname || !email || !password1 || !password2){
alert ("Blank field. Make sure all fields have been filled in.")
return false;
}
return true
}
function passwordIsString(){
var password1 = document.getElementById("password1").value;
if (typeof password1 !== "string"){
alert ("Password must be a string.")
return false;
}
return true;
}
function passwordsMatch(){
var password1 = document.getElementById("password1").value;
var password2 = document.getElementById("password2").value;
if (password1 !== password2){
alert ("Passwords do not match.")
return false;
}
return true;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment