1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Password Validation using HTML5

Discussion in 'Web Design, HTML And CSS' started by Ashish Bisht, Sep 9, 2016.

  1. Ashish Bisht

    Ashish Bisht New Member

    Joined:
    Aug 4, 2016
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hi, I am an application developer. While working on a project, I faced an issue related to validating password with regular expressions using JavaScript. I hope any tech developer may help me in resolving this issue. I have also figure out many mobile development forum to enhance my android & IOS programming knowledge & it's a great experience to learn skills from there.
     
  2. Ami Desai

    Ami Desai New Member

    Joined:
    Jan 5, 2017
    Messages:
    28
    Likes Received:
    13
    Trophy Points:
    3
    Location:
    Ahmedabad
    Home Page:
    Hi,

    You can check the following code

    HTML:
    <script type="text/javascript">
    
      function checkForm(form)
      {
        if(form.username.value == "") {
          alert("Error: Username cannot be blank!");
          form.username.focus();
          return false;
        }
        re = /^\w+$/;
        if(!re.test(form.username.value)) {
          alert("Error: Username must contain only letters, numbers and underscores!");
          form.username.focus();
          return false;
        }
    
        if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
          if(form.pwd1.value.length < 6) {
            alert("Error: Password must contain at least six characters!");
            form.pwd1.focus();
            return false;
          }
          if(form.pwd1.value == form.username.value) {
            alert("Error: Password must be different from Username!");
            form.pwd1.focus();
            return false;
          }
          re = /[0-9]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one number (0-9)!");
            form.pwd1.focus();
            return false;
          }
          re = /[a-z]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one lowercase letter (a-z)!");
            form.pwd1.focus();
            return false;
          }
          re = /[A-Z]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one uppercase letter (A-Z)!");
            form.pwd1.focus();
            return false;
          }
        } else {
          alert("Error: Please check that you've entered and confirmed your password!");
          form.pwd1.focus();
          return false;
        }
    
        alert("You entered a valid password: " + form.pwd1.value);
        return true;
      }
    
    </script>
    
    <form ... onsubmit="return checkForm(this);">
    <p>Username: <input type="text" name="username"></p>
    <p>Password: <input type="password" name="pwd1"></p>
    <p>Confirm Password: <input type="password" name="pwd2"></p>
    <p><input type="submit"></p>
    </form>

    Thanks
     
    Last edited: Jan 10, 2017

Share This Page