<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Face Chat Registration</title> <style> body { font-family: Arial, sans-serif; padding: 20px; background-color: #f4f4f4; } .container { max-width: 600px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } h1 { text-align: center; color: #333; } h2 { text-align: center; color: #333; font-size: 1.5em; } label { display: block; margin: 10px 0 5px; } input { width: 100%; padding: 8px; margin-bottom: 10px; border-radius: 4px; border: 1px solid #ccc; } button { padding: 10px 20px; background-color: #4CAF50; color: #fff; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #45a049; } .error { color: red; font-size: 14px; } .success { color: green; font-size: 14px; } </style> </head> <body> <div class="container"> <h1>Face Chat Registration</h1> <!-- Updated h2 tag --> <h2 id="heading">Face Chat</h2> <form id="registrationForm"> <label for="userName">Username:</label> <input type="text" id="userName" required><br><br> <label for="name">Name:</label> <input type="text" id="name" required><br><br> <label for="email">Email:</label> <input type="email" id="email" required><br><br> <label for="password">Password:</label> <input type="password" id="password" required><br><br> <label for="reEnter">Re-enter Password:</label> <input type="password" id="reEnter" required><br><br> <label for="mobile">Mobile Number:</label> <input type="text" id="mobile" required><br><br> <label for="age">Age:</label> <input type="number" id="age" required><br><br> <button type="button" id="register" onclick="validateRegistration()">Submit</button> </form> <p id="message" class="error"></p> <!-- Success and Result divs --> <div id="result" class="success" style="display: none;"></div> <div id="success" class="success" style="display: none;">Registration Successfull</div> <!-- Updated to match the required text --> </div> <script> function validateRegistration() { let userName = document.getElementById('userName').value; let name = document.getElementById('name').value; let email = document.getElementById('email').value; let password = document.getElementById('password').value; let reEnter = document.getElementById('reEnter').value; let mobile = document.getElementById('mobile').value; let age = document.getElementById('age').value; let messageElement = document.getElementById('message'); let resultElement = document.getElementById('result'); let successElement = document.getElementById('success'); try { // Validate that the age is a number and is above 18 if (age < 18 || isNaN(age)) { resultElement.style.display = 'block'; resultElement.textContent = 'You are too early to register in this site. Better you can wait for 6 years.'; throw new Error('You must be at least 18 years old to register.'); } // Validate username: simple check for alphanumeric characters if (!/^[a-zA-Z0-9]+$/.test(userName)) { throw new Error('Username should contain only alphanumeric characters.'); } // Validate password and re-enter password match if (password !== reEnter) { throw new Error('Password and re-entered password do not match.'); } // Validate mobile number format (simple check for 10 digits) if (!/^\d{10}$/.test(mobile)) { throw new Error('Please enter a valid 10-digit mobile number.'); } // If validation is successful, reset the message messageElement.textContent = ''; } catch (error) { // Handle errors (invalid input) messageElement.textContent = error.message; messageElement.className = 'error'; // Exit the function if there's an error return; } finally { // Always executed after the try/catch block if (age >= 18 && password === reEnter && /^\d{10}$/.test(mobile) && /^[a-zA-Z0-9]+$/.test(userName)) { resultElement.style.display = 'none'; // Hide result div when validation is successful successElement.style.display = 'block'; // Show success div messageElement.className = 'success'; document.getElementById('registrationForm').reset(); // Clear form after success } } } </script> </body> </html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter