Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 47 additions & 40 deletions form-validator/index.html
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css" />
<title>Form Validator</title>
</head>
<body>
<div class="container">
<form id="form" class="form">
<h2>Register With Us</h2>
<div class="form-control">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Enter username" />
<small>Error message</small>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="text" id="email" placeholder="Enter email" />
<small>Error message</small>
</div>
<div class="form-control">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Enter password" />
<small>Error message</small>
</div>
<div class="form-control">
<label for="password2">Confirm Password</label>
<input
type="password"
id="password2"
placeholder="Enter password again"
/>
<small>Error message</small>
</div>
<button type="submit">Submit</button>
</form>

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css" />
<title>Form Validator</title>
</head>

<body>
<div class="container">
<form id="form" class="form" autocomplete="off">
<h2>Register With Us</h2>

<div class="form-control">
<label for="username">Username</label>
<input type="text" id="username" placeholder="Enter username" autocomplete="off" />
<small>Error message</small>
</div>

<script src="script.js"></script>
</body>
</html>
<div class="form-control">
<label for="email">Email</label>
<input type="email" id="email" placeholder="example@gmail.com" autocomplete="off" />
<small>Error message</small>
</div>

<div class="form-control">
<label for="password">Password</label>
<input type="password" id="password" class="showPass" placeholder="Enter password" autocomplete="new-password" />
<button class="toggle-btn" type="button">Show</button>
<small>Error message</small>
</div>

<div class="form-control">
<label for="password2">Confirm Password</label>
<input type="password" id="password2" class="showPass" placeholder="Enter password again"
autocomplete="new-password" />
<button class="toggle-btn" type="button">Show</button>
<small>Error message</small>
</div>

<button type="submit" id="submit">Submit</button>
</form>
</div>
<script src="script.js"></script>

</body>

</html>
55 changes: 47 additions & 8 deletions form-validator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,34 @@ function checkLength(input, min, max) {
}
}

// show and hide password
function hidePass() {
const buttons = document.querySelectorAll(".toggle-btn");

buttons.forEach(function(btn) {
btn.addEventListener("click", function() {
// Find the input before this button
const inputPass = btn.previousElementSibling;

if (inputPass.type === "password") {
inputPass.type = "text";
btn.textContent = "Hide";
} else {
inputPass.type = "password";
btn.textContent = "Show";
}
});
});
}

hidePass();

// Check passwords match
function checkPasswordsMatch(input1, input2) {
if (input1.value !== input2.value) {
showError(input2, 'Passwords do not match');
}

}

// Get fieldname
Expand All @@ -75,12 +98,28 @@ function getFieldName(input) {
// Event listeners
form.addEventListener('submit', function(e) {
e.preventDefault();

if(checkRequired([username, email, password, password2])){
checkLength(username, 3, 15);
checkLength(password, 6, 25);
checkEmail(email);
checkPasswordsMatch(password, password2);

// Run all validations
checkRequired([username, email, password, password2]);
checkLength(username, 3, 15);
checkLength(password, 6, 25);
checkEmail(email);
checkPasswordsMatch(password, password2);

// Check if there are any errors
const errors = document.querySelectorAll('.form-control.error');

if (errors.length === 0) {
// No errors - all validations passed!
const submit = document.getElementById("submit");
submit.textContent = "Submitted successfully ✓";
submit.style.backgroundColor = "green";

// Optional: Reset form after 2 seconds
setTimeout(function() {
form.reset();
submit.textContent = "Submit";
submit.style.backgroundColor = ""; // Reset to original color
}, 2000);
}

});
});
24 changes: 22 additions & 2 deletions form-validator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ h2 {
}

.form {
padding: 30px 40px;
padding: 10px 10px;

}

.form-control {
Expand All @@ -41,6 +42,8 @@ h2 {
position: relative;
}



.form-control label {
color: #777;
display: block;
Expand All @@ -54,6 +57,23 @@ h2 {
width: 100%;
padding: 10px;
font-size: 14px;
position: relative;
}

.form-control .toggle-btn {
position: absolute;
right: 5px;
top: 32%;

transform: translateY(-50%);
background: transparent;
border: none;
cursor: pointer;
padding: 5px 10px;
color: black;

font-size: 11px;
width: 12%;
}

.form-control input:focus {
Expand Down Expand Up @@ -92,4 +112,4 @@ h2 {
padding: 10px;
margin-top: 20px;
width: 100%;
}
}