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>
Jie
  • 1,386
  • 3
  • 24
  • 60
  • 2
    You can't really do this via key events, because if there are ten inputs and one of them already contains the number "1" you wouldn't be able to enter the number "10" in another one (unless you pasted in "10" from the clipboard rather than typing a "1" followed by a "0"). – nnnnnn Aug 31 '17 at 02:40
  • 1
    IDs need to be unique, you can't have multiple `id="question[]"` inputs. – Barmar Aug 31 '17 at 02:40
  • @nnnnnn Indeed Sir. So, maybe you have an answer to my question? because I think you're a JavaScript master. – Jie Aug 31 '17 at 02:45
  • @Barmar Why Sir? Please state it, together with your answer, so we can figure it out – Jie Aug 31 '17 at 02:45
  • See https://stackoverflow.com/questions/14274982/how-can-i-apply-a-jquery-function-to-all-elements-with-the-same-id – Barmar Aug 31 '17 at 02:46
  • @Barmar Nice link. Anyway, do you have any solution with my problem now? – Jie Aug 31 '17 at 02:48
  • @Barmar I've updated my code now, changed `id` into `class`. – Jie Aug 31 '17 at 02:55
  • @nnnnnn Any solution you have Sir? – Jie Aug 31 '17 at 03:28

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
  • 5,672
  • 2
  • 25
  • 36
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
  • 11,650
  • 15
  • 23
  • 35
-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
  • 669,327
  • 51
  • 454
  • 560