1

So I am having trouble with input validation, or I would say having trouble with displaying certain text below the input when input is empty.

As you can see in the code I tried checking if the value of an input is '' but it dint now work I tried other things as well but. I would be very grateful if someone shows me my beginner mistakes.

$("#newPasswordTextBox").on("keyup", function() {
    let pass = $("#newPasswordTextBox").val();
    if (pass.length == '') {
      $("#newPasswordTextBox").html("Please enter your password");
    }
    if ((pass.length >= 10) && (pass.length < 15)) {
      var regex = /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[ `!#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~])/;

      if (!pass.match(regex)) {
        $("#newPasswordTextBox").css({
          "border-color": "red"
        })
      } else {
        $("#newPasswordTextBox").css({
          "border-color": "green"
        })
        $("#passwordCheck").html("Looks good");
        $("#passwordCheck").css({
          "color": "green"
        })
      }
    } else if (pass.length >= 15) {
      $("#newPasswordTextBox").css({
        "border-color": "green"
      })
    } else {
      $("#newPasswordTextBox").css({
        "border-color": "red"
      })
    }

    if (!this.pass) {
      $("#newPasswordTextBox").html("Please enter your password");
    }
  }

);

$("#confirmNewPasswordTextBox").on("keyup", function() {
  let pass = $("#confirmNewPasswordTextBox").val();
  let confpass = $("#newPasswordTextBox").val();

  if (pass === confpass) {
    $("#confirmNewPasswordTextBox").css({
      "border-color": "green"
    })
    $("#confirmPasswordCheck").html("Passwords are matching");
    $("#confirmPasswordCheck").css({
      "color": "green"
    })
  } else {
    $("#confirmNewPasswordTextBox").css({
      "border-color": "red"
    })
  }

  if (confpass.length == 0) {
    $("#confirmPasswordCheck").html("Please confirm your password");
    $("#confirmPasswordCheck").css({
      "color": "red"
    })
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
  <label for="newPasswordTextBox">New Password</label>
  <input type="password" id="newPasswordTextBox" class="form-control" name="password" placeholder="New Password" autocomplete="off">
  <span id="passwordCheck"></span>
</div>
<div class="form-group">
  <label for="confirmNewPasswordTextBox">Confirm Password</label>
  <input type="password" id="confirmNewPasswordTextBox" class="form-control" name="confirmNewPassword" placeholder="Confirm New Password" autocomplete="off">
  <span id="confirmPasswordCheck"></span>
</div>

1 Answer 1

1

Take a look at this block of code:

if (!this.pass) {
  $("#newPasswordTextBox").html("Please enter your password");
}

You're trying to set the html of the password input field. That won't work. An input field can only have the value set. I'm assuming you're trying to set the html of a label or div below the input field. Change the id to that element's id and it works.

if (!this.pass) {
  $("#someDivOrLabel").html("Please enter your password");
}

See here: https://jsfiddle.net/efb0xc3z/

Oh, and I modified your logic a little. Your original code was testing:

if (pass.length == "") { ... }

This will never be true since length will be an integer. You can just use:

if (!pass) { ... }

...to test if the password is not blank since any value other than '' will evaluate to true.

I also added some CSS to remove the focus border on the input field because that can obscure the outline color.

Sign up to request clarification or add additional context in comments.

6 Comments

OK, I have made a mistake when I have been trying many things, but your fiddle does not do thing I am looking for
I just made some edits. I'm using your logic so if that's not right, especially the regex test, I can't speak to what your intent is. But my code displays the text below the input field under certain conditions. If you type something then delete it all the text appears. Your code would never display text b/c it was trying to set .html() on an input field which is not valid.
Also, if you want to display the text "please enter your password" below the input field before someone starts typing, you need to put the logic in a function and call it when the page loads and then just call the function on keyup. The way it's written now, validation won't occur until typing starts.
I want to display it when input was deleted, and how would I do it for the confirm password
How would I do it for the conf password
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.