0

I would like to validate my input box for a password using javascript/jquery. The passwords needs to be 9 or more characters. I need to use keyup and count user input. If the input is under 9 characters there should be an error message under the input box. if there is more than 9 characters, then the error message should turn into a submit button. I kind of understand to get length, but I dont think I am doing it right. All help is appreciated. Here is some code:

< script >
  function checkInput(value) {
    document.getElementById('#password').value.length
    var minLength = 9;
    if (value.length < minLength) {
      //not sure
      else {

        return true;
      }
    }


    <
    /script>
<html>

<head>
  <title>Email List</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
      font-family: Arial;
      box-sizing: border-box
    }
    
    body {
      text-align: center;
    }
    
    header {
      padding: 50px;
      color: black;
      font-size: 20pt
    }
    
    section {
      width: 700px;
      margin: 0 auto;
    }
    
    ul {
      margin: 0;
      padding: 0;
      list-style: none;
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
    }
    
    ul li {
      cursor: pointer;
      position: relative;
      padding: 12px 8px 12px 40px;
      background: #FF6D1F;
      font-size: 18px;
      transition: 0.2s;
      text-align: left;
      user-select: none;
    }
    
    ul li:nth-child(odd) {
      background: #FA5B0F;
    }
    
    ul li:hover {
      background: #FF822E;
    }
    
    ul li.checked {
      background: #FF822E;
      color: #fff;
      text-decoration: line-through;
    }
    
    ul li.checked::before {
      content: '';
      position: absolute;
      border-color: #fff;
      border-style: solid;
      border-width: 0 2px 2px 0;
      top: 10px;
      left: 16px;
      transform: rotate(45deg);
      height: 15px;
      width: 7px;
    }
    
    .close {
      position: absolute;
      right: 0;
      top: 0;
      padding: 12px 16px 12px 16px;
      display: none;
      background-color: #f44336;
    }
    
    .close:hover {
      color: white;
    }
    
    form {
      padding: 0;
      margin-top: 20px;
      width: 100%;
      overflow: hidden;
    }
    
    input {
      height: 56px;
      line-height: 56px;
      margin-right: 0;
      display: inline-block;
      float: left;
      width: 90%;
      font-size: 18px;
      padding: 12px 8px 12px 40px;
      outline: 0;
    }
    
    .btn {
      display: inline-block;
      background-color: green;
      width: 10%;
      padding: 9.5px;
      font-size: 32px;
      cursor: pointer;
      background-color: #FA5B0F;
    }
    
    .btn:hover {
      color: white;
      background-color: #ff931e;
    }
    
    #lblError {
      background-color: #000;
      color: white;
      height: 56px;
      line-height: 56px;
      display: none;
    }
  </style>
</head>

<body>
  <header>
    <h1>Create A New Password</h1>
  </header>

  <section>


    <form>
      <input type="password" id="password" placeholder="Enter a password" autocomplete="off">

    </form>

    <div id="lblError">Please enter a password longer than 9 characters!</div>
  </section>

</html>

Feel free to ask for any clarification. Thank you!

Aniket G
  • 3,471
  • 1
  • 13
  • 39
SadGuy
  • 29
  • 4
  • https://stackoverflow.com/questions/10281962/is-there-a-minlength-validation-attribute-in-html5 – epascarello Feb 12 '19 at 00:45
  • You have your `checkInput` function but you're never calling it in the code you show -- you are not binding the keyup event to call the function. Dominic's answer (below) is pretty good, but I discourage using inline event handlers like his `onkeyup="checkInput()"` -- it's better to use `addEventListener()` as [MDN describes](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener) – Stephen P Feb 12 '19 at 01:09

2 Answers2

1

Use .on("keypress",function(){}) as written to the code below:

$("#password").on("keyup",function(){
    if($(this).val().length < 9 && $(this).val().length != 0 ){
      $("#lblError").prop("style","display:block");
      $("#Submit").prop("style","display:none");
    }
    else if($(this).val().length == 0){
      $("#Submit").prop("style","display:none");
      $("#lblError").prop("style","display:none");
    }
    else{
     $("#Submit").prop("style","display:block");
     $("#lblError").prop("style","display:none");
    }
})
<html>

<head>
  <title>Email List</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
      font-family: Arial;
      box-sizing: border-box
    }
    
    body {
      text-align: center;
    }
    
    header {
      padding: 50px;
      color: black;
      font-size: 20pt
    }
    
    section {
      width: 700px;
      margin: 0 auto;
    }
    
    ul {
      margin: 0;
      padding: 0;
      list-style: none;
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
    }
    
    ul li {
      cursor: pointer;
      position: relative;
      padding: 12px 8px 12px 40px;
      background: #FF6D1F;
      font-size: 18px;
      transition: 0.2s;
      text-align: left;
      user-select: none;
    }
    
    ul li:nth-child(odd) {
      background: #FA5B0F;
    }
    
    ul li:hover {
      background: #FF822E;
    }
    
    ul li.checked {
      background: #FF822E;
      color: #fff;
      text-decoration: line-through;
    }
    
    ul li.checked::before {
      content: '';
      position: absolute;
      border-color: #fff;
      border-style: solid;
      border-width: 0 2px 2px 0;
      top: 10px;
      left: 16px;
      transform: rotate(45deg);
      height: 15px;
      width: 7px;
    }
    
    .close {
      position: absolute;
      right: 0;
      top: 0;
      padding: 12px 16px 12px 16px;
      display: none;
      background-color: #f44336;
    }
    
    .close:hover {
      color: white;
    }
    
    form {
      padding: 0;
      margin-top: 20px;
      width: 100%;
      overflow: hidden;
    }
    
    input {
      height: 56px;
      line-height: 56px;
      margin-right: 0;
      display: inline-block;
      float: left;
      width: 90%;
      font-size: 18px;
      padding: 12px 8px 12px 40px;
      outline: 0;
    }
    
    .btn {
      display: inline-block;
      background-color: green;
      width: 10%;
      padding: 9.5px;
      font-size: 32px;
      cursor: pointer;
      background-color: #FA5B0F;
    }
    
    .btn:hover {
      color: white;
      background-color: #ff931e;
    }
    
    #lblError {
      background-color: #000;
      color: white;
      height: 56px;
      line-height: 56px;
      display: none;
    }
    #Submit {
      color: white;
      height: 56px;
      line-height: 56px;
      display: none;
    }
  </style>
</head>

<body>
  <header>
    <h1>Create A New Password</h1>
  </header>

  <section>


    <form>
      <input type="password" id="password" placeholder="Enter a password" autocomplete="off">

    </form>

    <div id="lblError">Please enter a password longer than 9 characters!</div>
    <div id="Submit"><input type="submit"></div>
  </section>

</html>

Feel free to ask for any clarification.

Jan Lois
  • 177
  • 9
1

You can also try using pure javascript to accomplish this.

var minLength = 9;
var output = document.getElementById("output");

function checkInput(){
    var password = document.getElementById("password").value;
    if (password.length < minLength) {
         output.innerHTML = "Not yet.....";
         return false; 
    }else {
         output.innerHTML = "<button>submit</button>";
      }
}
    <form>
      <input type="password" id="password" placeholder="Enter a password" autocomplete="off" onkeyup="checkInput()">
      <p id="output"></p>
    </form>