I got stacked with this problem:
- 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.
- 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>