0

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" />

1 Answer 1

1

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"/>

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I was about to answer the same answer, when I finished my fiddle you'd already given the answer :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.