Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Form Validation</title>
- </head>
- <body>
- <center>
- <form id="registrationForm">
- <h1>Registration Form</h1>
- <input type="text" id="mobile" name="mobile" placeholder="Mobile Number" required>
- <div id="mobileError" class="error"></div>
- <input type="email" id="email" name="email" placeholder="Email Address" required>
- <div id="emailError" class="error"></div>
- <input type="password" id="password" name="password" placeholder="Password" required>
- <input type="password" id="confirmPassword" name="confirmPassword" placeholder="Confirm Password" required>
- <div id="passwordError" class="error"></div>
- <button type="submit">Register</button>
- </form>
- </center>
- <script>
- function validateMobile(mobile) {
- const mobileRegex = /^[0-9]{10}$/;
- return mobileRegex.test(mobile);
- }
- function validateEmail(email) {
- const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
- return emailRegex.test(email);
- }
- function validatePassword(password, confirmPassword) {
- return password === confirmPassword;
- }
- document.getElementById('registrationForm').addEventListener('submit', function(event) {
- event.preventDefault();
- const mobile = document.getElementById('mobile').value;
- const email = document.getElementById('email').value;
- const password = document.getElementById('password').value;
- const confirmPassword = document.getElementById('confirmPassword').value;
- document.getElementById('mobileError').textContent = '';
- document.getElementById('emailError').textContent = '';
- document.getElementById('passwordError').textContent = '';
- let isValid = true;
- if (!validateMobile(mobile)) {
- document.getElementById('mobileError').textContent = 'Invalid mobile number. Must be 10 digits.';
- isValid = false;
- }
- if (!validateEmail(email)) {
- document.getElementById('emailError').textContent = 'Invalid email format.';
- isValid = false;
- }
- if (!validatePassword(password, confirmPassword)) {
- document.getElementById('passwordError').textContent = 'Passwords do not match.';
- isValid = false;
- }
- if (isValid) {
- alert('Form submitted successfully!');
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement