0

I am trying to check some multiple conditions in JQuery but it doesn't return the desired result. whatever I input it always went to else condition.

$(function() {
  $('#installment').on("keydown keyup", check);
  function check() {
    var inst = Number($("#installment").val());
    if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

4
  • What exactly are you expecting and doesn’t happen? Commented Mar 28, 2019 at 7:12
  • If entering 12 doesn’t work, that’s because when entering the digit 1 you clear the input. With this type of approach you’ll only be able to validate 1 char input, or inputs where you add the entire prefixes. Another note, val returns a string and you are strict checking. Commented Mar 28, 2019 at 7:15
  • @RaduDiță "val returns a string" - that's why it has Number() around it Commented Mar 28, 2019 at 7:17
  • @freedomn-m My bad, missed that Commented Mar 28, 2019 at 7:23

4 Answers 4

1

The problem is in the keydown triggered before the value update. So if you enter '2'. The value will return '' And Number('') is 0 so it always go to else Just remove the keydown event only use keyup

$(function() {
  $('#installment').on("keyup", check);

  function check() {
    var inst = Number($("#installment").val());
    console.log(inst)
    if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

As @Radu Diță mentioned in the comment that 12 can never be valid if 1 is not so to fix that you can use String.prototype.startsWith()

$(function() {
  $('#installment').on("keyup", check);
  function check() {
    var inst = Number($("#installment").val());
    console.log(inst)
    if (inst === 2 || inst === 4 || inst === 6 || '12'.startsWith(String(inst))) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

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

4 Comments

Or use input event to also pick up other input methods (just not keydown as detailed here)
Key up will not solve the entire problem. 12 will never be validated as 1 is not valid.
@Radu Diță I have attempted to fix that problem. Please see the edit. Thanks for pointing out
Not much else you can do without a major change to how this works, but OP needs to be aware that there needs to be a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
0

Try this:

$(function() {
  $('#installment').on("keydown keyup", check);
  function check(event) {
    var inst = $(this).val();
    if (inst === "") {
        return;
    }
    if (inst == 2 || inst == 4 || inst == 6 || inst == 12) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

In this code if starts working but only for 2, 4 and 6. It will not work for 12 as the first input is 1 which you doesn't validate instead of validating the input in keydown and keyup try to use blur once user finish writing something then validation works as you you like to.

$(function() {
  $('#installment').on("blur", check);
  function check(event) {
    var inst = Number($(this).val());
    if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

Comments

0

The issue is that your keydown event fires before the value of the input is modified. Use keyup (or better yet, input) instead:

$(function() {
  $('#installment').on("input", check);

  function check() {
    var inst = parseInt($("#installment").val());
    if (inst == 2 || inst == 4 || inst == 6 || inst == 12) {
      return true;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      return false;
    }
  }
});
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<tr>
  <td>Number of Installment</td>
  <td><input type="text" name="installment" id="installment"></td>
</tr>

3 Comments

But required if 12 should be allowed as a valid answer @freedomn-m.
OK then @freedomn-m I'll remove that.
TBH, in this case, I'm not sure what else can be done with OPs limit code (so I've removed my previous comments) - it would need to have a secondary validation to ensure 1 doesn't pass on in the process (but should have this anyway as javascript shouldn't be the only validation, it's a UI-nicety / first-catch only)
0
$(function() {
    var val = $("#installment").val();
  $('#installment').on("keydown keyup",
  function () {
    var inst = Number($("#installment").val());
    if (inst === 2 || inst === 4 || inst === 6 || inst === 12) {
      alert("OK");
     return false;
    } else {
      $("#installment").val('');
      alert("you can not set wrong value");
      //return false;
    }
  });
});

Comments

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.