0

please advise me how one can validate multiple dropdowns in a form, validation should trigger if same value is selected in different dropdowns.

here, if i select same option from both the dropdown it should trigger validation. (i am using jquery validation engine for form validations)

<select name="user[role_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">Administrator</option>
  <option value="2">User</option>
  <option value="3">Editor</option>
</select>

<select name="user[role_id]">
  <option value="">Please select</option>
  <option value="1" selected="selected">Administrator</option>
  <option value="2">User</option>
  <option value="3">Editor</option>
</select>

thanks

2 Answers 2

3

Easiest way is to map the selections and look for dupes. There are better algorithms to look for dupes (i.e. exit as soon as you detect a duplicate), but I opted to use shorter code since the cost is very low: http://jsfiddle.net/rkw79/vqENk/

function HasDupe(){
    a = $('select').map(function(i,o) {
        return $(o).val();
    });
    return (a.length != $.unique(a).length);
}
Sign up to request clarification or add additional context in comments.

Comments

0
<select name="user[role_id]">
   <option value="0">Please select</option>
   <option value="1" selected="selected">Administrator</option>
   <option value="2">User</option>
   <option value="3">Editor</option>
</select>

<select name="user[role_id]">
   <option value="0">Please select</option>
   <option value="1" selected="selected">Administrator</option>
   <option value="2">User</option>
   <option value="3">Editor</option>
</select>
<script type="text/javascript">
  $(document).ready(function(e){
          if ($('select[name="user[role_id1]"]').val() == $('select[name="user[role_id2]"]').val()) {
        alert('select different value');//validation msg
    }
 });
</script>

Comments

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.