-1

I'm looking to make an input validation. Basically, I have a form with 5 text field but the first two should have a condition: At least one of the two must be completed.

Currently the validation I use jQuery validate, but that does not fulfill this function, would you have any idea?

Thank you :)

Stéphane
  • 23
  • 4

2 Answers2

1

Quote OP:

"At least one of the two must be completed.

Currently the validation I use jQuery validate, but that does not fulfill this function, would you have any idea?"

Incorrect... it does have this ability. See documentation: http://jqueryvalidation.org

It's called the require_from_group method included in the additional-methods.js file. Usage as follows:

1) Give each item in the group a common class.

HTML:

<input class="productinfo" name="partnumber">
<input class="productinfo" name="description">

2) Define the rule as follows, where the first parameter is the minimum number required from the group and the second is the class you created above.

jQuery:

$(document).ready(function(){

    $('#myform').validate({
        rules: {
            partnumber:  {
                require_from_group: [1,'.productinfo']
            },
            description: {
                require_from_group: [1,'.productinfo']
            }
        },
        groups: {
            mygroup:  "partnumber description"
        }
    });

});

Now, at least partnumber OR description must be filled out.

3) Optionally, to clean up the duplication of error messages, use the groups option as follows, which will lump the error messages for the two inputs above into one.

groups: {
    mygroup:  "partnumber description"
}
Sparky
  • 98,165
  • 25
  • 199
  • 285
0

Once the person has moved to third field(use onfocus to determine this), you can validate it.Check if one of the two fields is filled.If not report it and focus on the first field.

Aravind
  • 3,169
  • 3
  • 23
  • 37
  • Hi, Yes i think it's just a provided in JS, but i don't know how i can to make it work, actualy i test with this :
    if(input.value != null || input2.value != null)
    but no result :(
    – Stéphane Jun 22 '13 at 08:33
  • Why don't you post your code on jsfiddle, so I can help better? – Aravind Jun 22 '13 at 08:47