4

I need to validate bunch of text boxes which has dynamic ID. The elements are available on document ready, not dynamically inserted.

<input type="text" class="y"/>
<input type="text" class="y"/>
<input type="text" class="y"/>
<input type="text" class="y"/>
<input type="text" class="y"/>

The rule is that one of the first two field is required. I'm lost how to validate without knowing ID of the element.

EDIT: Input elements are generated by loop so I cant add class required for first two. I'm looking for a solution using jquery Validation plugin http://docs.jquery.com/Plugins/Validation

4
  • Always first 2 elements or requirement is changed? Commented Dec 24, 2012 at 7:03
  • What criteria do you choose which one needs to be validated? Commented Dec 24, 2012 at 7:05
  • don't input's have a name in order to submit? Commented Dec 24, 2012 at 7:08
  • Always first 2 to be validated. I skipped name and other attributes for clarification. Commented Dec 24, 2012 at 8:59

5 Answers 5

2

try this

$(document).ready(function() {
if($('input:text:eq(0)').val()=="" && $('input:text:eq(1)').val()==""){
 //show error
}
});
Sign up to request clarification or add additional context in comments.

5 Comments

there is no :second selector use :eq(index) selector or eq(index) method
close...needs to be one or other is valid... so use || instead of && in the if and input doesn't have text() method..use val()
@charlietfl:but he wants either one of them is mandatory...then im guessing that if both fields are empty,it should throw error?
oops..my bad.. you are right.. small hours of the morning here
OP says he's looking for solution using jQuery Validate plugin.
2

Following uses filter to create collection of the 2 inputs that value is not empty.. Testing the length provides a validity test

var valid=$('input').slice(0,2).filter(function(){
   return $(this).val() !='';
}).length

if(valid){
  /* can submit*/
}

Solution for validation plugin:

 $('input:lt(2)').addClass('required');

NOTE: adjust selector to fit form better so it doesn't find every input in page, or inputs in form that are not part of the collection you want to validate. you could wrap these inputs in a fieldset and give fieldset an ID

1 Comment

OK...that's simple enough by adding class required. I updated answer. You can use classes with validation plugin, or write rules object.
1
$(document).ready(function() {
if($('input:text:lt(2)').val()=="" ){
 //error 
}
});

1 Comment

won't quite work val() will only return value of first element
1

I think that what you want is this:

function isValid(){
    var cont=0;
    $('input:text').each(function(i, obj) {
        if(i<2 && obj.value){
             cont++; 
        }
      });
    return cont==2;
}

Comments

1

try This :

You can do it using :lt(); like this:

$("form input:lt(2)");

This selects all elements that match at less-than the passed index, the first and second elements are index 0 and 1, and will match this selector :)

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.