0

I got stacked with this problem:

  1. I just want to check if the number I entered is already entered in a dynamic inputs, then if it is, you cannot entered that number again, unless you delete it.
  2. You can only enter a number up to the number of inputs (e.g. if you have 10 inputs, then you can only enter NOT greater than 10)

$(document).ready(function(){
  var arrayLen = $('.question').length;
  var numArray = [];
  var convertedArray;
  for(i = 1; i <= arrayLen; i++){
   numArray.push(i);
  }
 
  var currentVal;
  var maxAllowed = numArray[numArray.length - 1];
  var hasValue = [];
   
  $('.question').on('input', function(){
    currentVal = this.value;
    this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
  }).bind('keyup', function(){
   if(currentVal <= maxAllowed){
     $("#result").html("available");
    } else{
     $("#result").html("not available");
      return false;
    }
  });
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="description"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>

<span id="result"></span>
Blues Clues
  • 1,694
  • 3
  • 31
  • 72

3 Answers3

0

I did not do any validation or control what action to happen when your conditions are satisfied.

$(document).ready(function() {

  var $inputObj = $("#input-div input[type=text]");
  var inputCount = $inputObj.length;

  // alert(inputCount);

  $('#question\\[\\]').on('input', function() {

    var $currInput = $(this);
    var currInputVal = $currInput.val();
    console.log(currInputVal);

    var cond1 = currInputVal < inputCount;
    var cond2 = isFreshInput($currInput);

    //console.log("cond2:" + cond2);
    if (cond1 === false || cond2 === false) {
      //TODO action when conditions are satisfied       
      $("#result").html("not available");
      alert("Either value already exist or number is too large");
    } else {
      $("#result").html("available");
    }

  });

  function isFreshInput($currInput) {

    var currInputVal = Number($currInput.val());
    var res = true;

    $inputObj.not($currInput).each(function() {

      let inputVal = Number($(this).val());

      console.log("inputVal:" + inputVal);


      if (currInputVal === inputVal) {
        console.log("input already exist!");
        $(this).css({
          "border": "solid red 1px"
        })
        res = false;
        // return false;  // break loop once found
      }
    });
    console.log("res:" + res);
    return res;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="input-div">
  <input type="text" id="question[]" placeholder="multipleChoice"><br><br>
  <input type="text" id="question[]" placeholder="trueFalse"><br><br>
  <input type="text" id="question[]" placeholder="shortAnswer"><br><br>
  <input type="text" id="question[]" placeholder="shortAnswer"><br><br>
  <input type="text" id="question[]" placeholder="description"><br><br>
  <input type="text" id="question[]" placeholder="multipleChoice"><br><br>
  <input type="text" id="question[]" placeholder="multipleChoice"><br><br>
  <input type="text" id="question[]" placeholder="trueFalse"><br><br>
  <input type="text" id="question[]" placeholder="trueFalse"><br><br>
  <input type="text" id="question[]" placeholder="multipleChoice"><br><br>
  <input type="text" id="question[]" placeholder="multipleChoice"><br><br>
</div>
<span id="result"></span>
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38
0

Here you go with a solution https://jsfiddle.net/33zeL2fa/9/

$(document).ready(function(){
  var arrayLen = $('input.question').length;
  var numArray = [];
  var convertedArray = [];
  for(i = 1; i <= arrayLen; i++){
   numArray.push(i);
  }

  $('input.question').keydown(function(e){
  var val = $(this).val();
   if(e.which === 8 || e.keyCode === 8){
     numArray.push(parseInt($(this).attr('newval')));
      $(this).removeAttr('newval');
    } else {
     val += e.key;
      if(numArray.indexOf(parseInt(val)) != -1){
        $('#result').html("available");
        $(this).attr('newval', e.key);
        numArray.splice(numArray.indexOf(parseInt(val)), 1);
      } else{
        $("#result").html("not available");
        e.preventDefault();
      }
    }
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="description"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>

<span id="result"></span>

Hope this will help you.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
-1

Don't do this in the input event, because when you're in the middle of typing an input it may match another input, and you won't let them finish editing it. You should use the blur event; this fires when they try to leave the input field.

When they change the input, loop through all the other inputs, and check if this value is the same as any of the others. If it is, show the error.

$(document).ready(function() {
  var maxAllowed = $(".question").length;
  $('.question').on('blur', function() {
    var currentVal = this.value.trim();
    if (currentVal == '') {
      return;
    }
    var msg = "Available";
    if (currentVal < 1 || currentVal > maxAllowed) {
      msg = "Not available";
    } else {
      $('.question').not(this).each(function() {
        if (this.value == currentVal) {
          msg = "Not available";
          return false; // Stop the .each loop
        }
      });
    }
    $("#result").text(msg);
    if (msg == "Not available") {
      $(this).focus(); // Put them back in the bad input field
      return false;
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="shortAnswer"><br><br>
<input type="text" class="question" placeholder="description"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="trueFalse"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>
<input type="text" class="question" placeholder="multipleChoice"><br><br>

<span id="result"></span>
Barmar
  • 741,623
  • 53
  • 500
  • 612