rahul.m
rahul.m

Reputation: 5854

JQuery check multiple inputs value is not same

I have multiple input boxes with the same class name, I want to check whether every input box has a unique value in it (number). I want a unique sequence number in every input.

$('.seq_box').change(function() {
  alert('number this already exist')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="seq_box" />
<input type="number" class="seq_box" />
<input type="number" class="seq_box" />
<input type="number" class="seq_box" />
<input type="number" class="seq_box" />

Upvotes: 0

Views: 380

Answers (1)

mplungjan
mplungjan

Reputation: 178400

Perhaps this?

$('.seq_box').on("change",function(e) {
  const vals = $('.seq_box').not(this).map(function() { return this.value }).get()
  const that = this;
  if (vals.indexOf(this.value) !=-1) {
    console.log('This number already exists')
    that.value="";
    setTimeout(function() { that.focus() },100)
  }  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="seq_box"/>
<input type="number" class="seq_box"/>
<input type="number" class="seq_box"/>
<input type="number" class="seq_box"/>
<input type="number" class="seq_box"/>

Upvotes: 1

Related Questions