Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

47
Views
Problem when validating an input with javascript

Currently I am validating an input of type number with javascript, this must be between 2 and 12, but it does not allow me to choose between 10 or 11.

$(".m-max-212").on("input", function() {
  var value = $(this).val();
  if (value !== "" && value.indexOf(".") === -1) {
    $(this).val(Math.max(Math.min(value, 12), 1));
    $(".cm-max-299").val(0);
  }
});

$(".cm-max-299").on("input", function() {
  var value = $(this).val();
  if ($(".m-max-212").val() < 12) {
    if (value !== "" && value.indexOf(".") === -1) {
      $(this).val(Math.max(Math.min(value, 99), 0));
    }
  } else {
    $(this).val(0);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input type="number" class="m-max-212" placeholder="">
</div>
<div class="input-group mb-2">
  <input type="number" class="cm-max-299" placeholder="">
</div>

What am I doing wrong?

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs