0

I am trying to check if there is at least 1 digit in an input field. If there is I want to change the default image.

I am getting an error with the .isNumeric function. I know in the docs it shows $.isNumeric, but I am not sure how to add that within hasNumber.isNumeric().length >= 1;.

I am open to a different function if it will allow this to work.

$('#register').keyup(function() {
  var hasNumber = $("#password").val();
  var hasNumberValid = hasNumber.isNumeric().length >= 1;
  $('#upperCase').attr('src', hasNumberValid ? 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQlyqJ14HYP1WclpK9RkJWo8jIDBkhTW0GS31AxRkozAEA72ULhY89LIzk' : 'icons/collection/delete.png');
});
#password-check {
  margin: 30px auto;
}
.password-check-field {
  color: black;
}
.password-check-field img {
  margin-right: 15px;
  height: 15px;
  width: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="" method="POST" id="register">
  <div class="field">
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" required>
  </div>
  <div class="field">
    <label for="password">Choose a password</label>
    <input type="password" name="password" id="password" required>
  </div>
  <div class="password-check-field">
    <img id="upperCase" src="icons/collection/delete.png" alt="Success">Your password has at a number in it
  </div>
</form>

6
  • 3
    Try $.isNumeric(hasNumber) Commented Dec 16, 2016 at 14:59
  • 1
    var isNumeric = $.isNumeric(value_to_check); Commented Dec 16, 2016 at 14:59
  • 1
    api.jquery.com/jQuery.isNumeric . try $.isNumeric(hasNumber) Commented Dec 16, 2016 at 15:00
  • "I know in the docs it shows $.isNumeric"... so use that? Commented Dec 16, 2016 at 15:00
  • 1
    @j08691 Thanks. That did the trick! Feel free to leave an answer. Commented Dec 16, 2016 at 15:03

5 Answers 5

2

$.isNumeric() accepts an argument of any value, so you can pass in your $("#password").val() to it via:

$.isNumeric(hasNumber) or $.isNumeric( $("#password").val() )

The $.isNumeric() method checks whether its argument represents a numeric value. If so, it returns true. Otherwise it returns false. The argument can be of any type.

No need to use length on that one.

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

Comments

1

Try like this.

$('#register').keyup(function() {
  var hasNumber = $("#password").val();
  var hasNumberValid = false;
  
  var inputArray = hasNumber.split('');
  
  inputArray.forEach(function(element) {
    if($.isNumeric(element))
      {
         hasNumberValid = true
       }
   });
  

  $('#upperCase').attr('src', hasNumberValid ? 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQlyqJ14HYP1WclpK9RkJWo8jIDBkhTW0GS31AxRkozAEA72ULhY89LIzk' : 'icons/collection/delete.png');
});
#password-check {
  margin: 30px auto;
}
.password-check-field {
  color: black;
}
.password-check-field img {
  margin-right: 15px;
  height: 15px;
  width: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="" method="POST" id="register">
  <div class="field">
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" required>
  </div>
  <div class="field">
    <label for="password">Choose a password</label>
    <input type="password" name="password" id="password" required>
  </div>
  <div class="password-check-field">
    <img id="upperCase" src="icons/collection/delete.png" alt="Success">Your password has at a number in it
  </div>
</form>

Comments

0

isNumeric() returns a boolean. Check to see if it's true. Try using $.isNumeric()

var hasNumberValid = $.isNumeric(hasNumber)

Comments

0

You can use a simple regex in order to check if there is a number in your string

var hasNumberValid = hasNumber.match(/\d+/g) != null;

Comments

0

I would put the keyup on your password field instead of the form and use a regular expression instead of isNumericn since the documentation states that "Description: Determines whether its argument represents a JavaScript number." I'm not sure whether it will be true in the case where you have both numbers and letters.

fiddle example

var AtLestOneLetterAndOneNumberRegex = new RegExp(".*[0-9].*");
$('#password').keyup(function() {
  var passwordValue = $("#password").val();
  if (AtLestOneLetterAndOneNumberRegex.test(passwordValue)){

    $("#validPasswordMessage").css("visibility","visible");
  }

});

1 Comment

I did not rename the long variable, I apologize, this is only checking for one number it's not checking for letters

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.