jquery-validation / jquery-validation

jQuery Validation Plugin library sources

Home Page:https://jqueryvalidation.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ability to require at least one of multiple validation methods

rodrigoaguilera opened this issue · comments

New feature motivation

I have an input that accepts Spanish nie and Spanish nif. Specifiying one of the formats is enough to pass validation.

New feature description

Have a new "metamethod" that allows for defining this kind of behavior

New feature implementation

Right now the format for rules "ands" all the rules as far as I know like

  rules: {
    // simple rule, converted to {required:true}
    name: "required",
    // compound rule
    email: {
      required: true,
      email: true
    }
  }

The email needs to pass required AND email methods.

A suggestion on how it could look like in the configuration:

  rules: {
    nifcif: {
      oneof: {
        nifES: true,
        nieES: true
      }
    }
  }

Maybe there is already a way to define this but I'm not aware of it.

My current workaround is to a have a new method like this

jQuery.validator.addMethod( "nifnieES", function( value, element ) {
  "use strict";

  return jQuery.validator.methods.nifES.call(this, value, element) || jQuery.validator.methods.nieES.call(this, value, element);

}, "Please specify a valid NIF/NIE number." );

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automatically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)

If this project is maintained I would like to get a comment on the feature

commented

The suggested workaround is how this should be implemented.

Ok. Is it possible to suggest this as an addition to the documentation? I'm sure my future self will face this problem again and will forget how to do it.

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automatically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)

I proposed the doc changes
jquery-validation/validation-content#57

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automatically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)

commented

I'm going to close this now that the PR has been created and linked.