Advertisement
vvccs

wt_3_js_validation

Oct 13th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Form Validation</title>
  7. </head>
  8. <body>
  9.     <center>
  10.     <form id="registrationForm">
  11.         <h1>Registration Form</h1>
  12.         <input type="text" id="mobile" name="mobile" placeholder="Mobile Number" required>
  13.         <div id="mobileError" class="error"></div>
  14.         <input type="email" id="email" name="email" placeholder="Email Address" required>
  15.         <div id="emailError" class="error"></div>
  16.         <input type="password" id="password" name="password" placeholder="Password" required>
  17.         <input type="password" id="confirmPassword" name="confirmPassword" placeholder="Confirm Password" required>
  18.         <div id="passwordError" class="error"></div>
  19.         <button type="submit">Register</button>
  20.     </form>
  21.     </center>
  22.  
  23.     <script>
  24.         function validateMobile(mobile) {
  25.             const mobileRegex = /^[0-9]{10}$/;
  26.             return mobileRegex.test(mobile);
  27.         }
  28.         function validateEmail(email) {
  29.             const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  30.             return emailRegex.test(email);
  31.         }
  32.         function validatePassword(password, confirmPassword) {
  33.             return password === confirmPassword;
  34.         }
  35.         document.getElementById('registrationForm').addEventListener('submit', function(event) {
  36.             event.preventDefault();
  37.             const mobile = document.getElementById('mobile').value;
  38.             const email = document.getElementById('email').value;
  39.             const password = document.getElementById('password').value;
  40.             const confirmPassword = document.getElementById('confirmPassword').value;
  41.             document.getElementById('mobileError').textContent = '';
  42.             document.getElementById('emailError').textContent = '';
  43.             document.getElementById('passwordError').textContent = '';
  44.             let isValid = true;
  45.             if (!validateMobile(mobile)) {
  46.                 document.getElementById('mobileError').textContent = 'Invalid mobile number. Must be 10 digits.';
  47.                 isValid = false;
  48.             }
  49.             if (!validateEmail(email)) {
  50.                 document.getElementById('emailError').textContent = 'Invalid email format.';
  51.                 isValid = false;
  52.             }
  53.             if (!validatePassword(password, confirmPassword)) {
  54.                 document.getElementById('passwordError').textContent = 'Passwords do not match.';
  55.                 isValid = false;
  56.             }
  57.             if (isValid) {
  58.                 alert('Form submitted successfully!');
  59.             }
  60.         });
  61.     </script>
  62.  
  63. </body>
  64. </html>
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement