HTML CODE
CSS CODE
<!DOCTYPE html> <html> <head> <title>Login and Registration Form</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <div class="form-container"> <form id="login-form" class="login-form"> <h2>Login</h2> <input type="text" placeholder="Username" id="login-username" required> <input type="password" placeholder="Password" id="login-password" required> <button type="submit">Login</button> </form> <form id="register-form" class="register-form"> <h2>Register</h2> <input type="text" placeholder="Username" id="register-username" required> <input type="password" placeholder="Password" id="register-password" required> <button type="submit">Register</button> </form> </div> <div class="message" id="message"></div> </div> <script src="script.js"></script> </body> </html>
JavaScript CODE
.container { display: flex; justify-content: center; align-items: center; height: 100vh; } .form-container { width: 300px; } form { display: none; } h2 { text-align: center; } input { display: block; width: 100%; margin-bottom: 10px; padding: 5px; } button { display: block; width: 100%; padding: 10px; background-color: #4CAF50; color: white; border: none; cursor: pointer; } .message { text-align: center; margin-top: 10px; }
document.getElementById("register-form-link").addEventListener("click", function(e) { document.getElementById("login-form").style.display = "none"; document.getElementById("register-form").style.display = "block"; document.getElementById("message").innerText = ""; e.preventDefault(); }); document.getElementById("login-form-link").addEventListener("click", function(e) { document.getElementById("register-form").style.display = "none"; document.getElementById("login-form").style.display = "block"; document.getElementById("message").innerText = ""; e.preventDefault(); }); document.getElementById("register-form").addEventListener("submit", function(e) { e.preventDefault(); var username = document.getElementById("register-username").value; var password = document.getElementById("register-password").value; // Perform registration logic here (e.g., send data to server) document.getElementById("register-username").value = ""; document.getElementById("register-password").value = ""; document.getElementById("message").innerText = "Registration successful. You can now log in."; }); document.getElementById("login-form").addEventListener("submit", function(e) { e.preventDefault(); var username = document.getElementById("login-username").value; var password = document.getElementById("login-password").value; // Perform login logic here (e.g., send data to server and validate credentials) document.getElementById("login-username").value = ""; document.getElementById("login-password").value = ""; document.getElementById("message").innerText = "Login successful."; });