Skip to content

Instantly share code, notes, and snippets.

@BhoiDanny
Last active May 20, 2023 07:04
Show Gist options
  • Select an option

  • Save BhoiDanny/44b7b288f08ae0861be14777520685a3 to your computer and use it in GitHub Desktop.

Select an option

Save BhoiDanny/44b7b288f08ae0861be14777520685a3 to your computer and use it in GitHub Desktop.
Write a html and javascript code to create a form and perform validation on for the figure below. This form has four input fields: name, email, password, and confirmPassword, the validateForm() function is called when the form is submitted, and it checks if all the fields are filled out correctly. if a field is not filled out correctly , an aler…
<!DOCTYPE html>
<html>
<head>
<title>Form Validation Example</title>
<script>
function validateForm() {
var name = document.forms["myForm"]["name"].value;
var email = document.forms["myForm"]["email"].value;
var password = document.forms["myForm"]["password"].value;
var confirmPassword = document.forms["myForm"]["confirmPassword"].value;
// Check if any field is empty
if (name == "" || email == "" || password == "" || confirmPassword == "") {
alert("All fields must be filled out");
return false;
}
// Check if password and confirm password match
if (password !== confirmPassword) {
alert("Passwords do not match");
return false;
}
// All validations pass, form can be submitted
return true;
}
</script>
</head>
<body>
<h1>Form Validation Example</h1>
<form name="myForm" onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment