Email&Password validation

    0

    1

    sznix

    The code in this example validates a user's email and password. If both are valid, the user is allowed to submit the form. If one of the inputs is not valid, an error is displayed and the user is not allowed to submit the form.

    //get id element from index.html and return
    
    //make sure the entered email is valid if not then ask the user to enter a valid email
    function validateEmail() {
      const email = document.getElementById('email').value;
      const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      if (regex.test(email)) {
        return true;
      } else {
        alert('Please enter a valid email');
        return false;
        }
    }
    
    //make sure entered password is correct regex
    function validatePassword() {
      const password = document.getElementById('ps').value;
      const regex = /^(?=.*\d)(?=.*[a-z])(?=.* [A-Z]).{6,20}$/;
      if (regex.test(password)) {
        return true;
      } else {
          alert('Password must have at least: 1 lowercase, 1 uppercase, 1 number and be between 6 and 20 characters');
        return false;
        document.getElementById('ps').value = '';
    
      }
    }
    //don't let the user submitting their password if it is not valid
    
    function validateForm() {
      const email = document.getElementById('email').value;
      const password = document.getElementById('ps').value;
      if (validateEmail() && validatePassword()) {
        document.getElementById('submit').disabled = false;
        alert('Please enter a valid email or password');
    
        //and clear the password field
        //document.getElementById('ps').value = '';
        //return false;
      }
    }
    
    // function validatePassword2() {
    //     const password = document.getElementById('ps').value;
    //     const confirmPassword = document.getElementById('ps2').value;
    //     if (password === confirmPassword) {
    //         return true;
    //     } else {
    //         alert('Passwords do not match');
    //         return false;
    //     }
    // }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.