Jahlil Espinoza
Jahlil Espinoza

Reputation: 83

Check checkbox when textbox have specific number of characters

Hi im trying to make that my checkbox checked when the user type 8 digits on my input textbox. Now i can make the checkbox checked when te user types but im new in this and can't figure it out. THX for the help.

<input id="thecheckbox" type="checkbox">
<input id="theinput" type="text">

    $("#theinput").keyup(function () {
    if ($(this).val() == "") {
        $("#thecheckbox").prop("checked", false);
    } else {
        $("#thecheckbox").prop("checked", true);
    }
});

$("#thecheckbox").change(function () {
    if (!$(this).is(':checked')) {
        $("#theinput").val("");
    }
});

Upvotes: 0

Views: 66

Answers (2)

Joseph Marikle
Joseph Marikle

Reputation: 78520

Do you mean something like this?

$("#theinput").on('input', function () {
  if (this.value.length >= 8) {
    $("#thecheckbox").prop("checked", true);
  } else {
    $("#thecheckbox").prop("checked", false);
  }
});

/* $("#thecheckbox").change(function () {
  if (!$(this).is(':checked')) {
    $("#theinput").val("");
  }
}); */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="thecheckbox" type="checkbox">
<input id="theinput" type="text">

Upvotes: 1

dokgu
dokgu

Reputation: 6040

Instead of $(this).val() == "" try using $(this).val().length != 8.

Upvotes: 2

Related Questions