1

I need to validate the floor fields that mention below.

<form id="test_form" >
<input name="floor[abc]" type="text" class="input_txt_l " value="" />
<input name="floor[cde]" type="text" class="input_txt_l " value="" />
</form>

I tried with jquery validator plug-in.

jQuery("#test_form").validate({
    rules: {
    'floor[]':{
        required:true
    }},
messages: {
        'floor[]':{
            required:"floor is required."
            }
     }
    });

In here validation is not working. but if the floor has no index , It works well. If someone have idea to fix this issue please help me.

6
  • 2
    try changing name=floor[] on both the input fields. What you might want is do have same validation for all the floor. Commented Dec 9, 2014 at 8:48
  • @axel.michel I have looked this. but with floor[index] it doesn't work. Commented Dec 9, 2014 at 9:10
  • @Anshul Nigam It can't change as floor[], Need index for floor Commented Dec 9, 2014 at 9:11
  • then use like floor[][ID] Commented Dec 9, 2014 at 9:16
  • What is your exact requirement, why do you need index? Commented Dec 9, 2014 at 9:22

1 Answer 1

4

There are different ways to solve your problem:

Either you use addClassRules:

// using the class name instead of field name
jQuery.validator.addClassRules("input_txt_l", {
    required: true       
});

Or you handle each field separately, because in fact they are named different:

jQuery("#test_form").validate({
    rules: {
        'floor[abc]':{
            required:true
        },
        'floor[cde]':{
            required:true
        },
    }
});

Or you rename your fields:

HTML:

<input name="floor[][abc]" type="text" class="input_txt_l " value="" />
<input name="floor[][cde]" type="text" class="input_txt_l " value="" />

JavaScript:

jQuery("#test_form").validate({
    rules: {
    'floor[]':{
        required:true
    }},
});

fiddle is here.

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

3 Comments

what if only one is mandatory ??
@Bugfixer I am not sure if I get your question correctly, but in case you have just one exception you could go for something like floor[]:{required:false} and the specific field floor['fieldname']:{required:true}
I tried this and still not working. But then I figured out that I missed the quote for input field names and it works now. Thanks for this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.