Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Possible to use "or"? #98

Closed
pcxstephan opened this issue Dec 18, 2015 · 3 comments
Closed

Possible to use "or"? #98

pcxstephan opened this issue Dec 18, 2015 · 3 comments

Comments

@pcxstephan
Copy link

Hi!

I want to validate an IP address field as either IPv4 OR IPv6. Using the directive I tried validate="ipv4|ipv6" but this makes it so that the field has to be valid for IPv4 AND IPv6. Is this possible to do without writing a custom validation function?

@ghiscoding
Copy link
Owner

Angular-Validation is looping through all Validators in sequence and they all need to be valid for the field to become valid itself. Basically there is currently no such thing as either "or" validator.

In your case, it's probably best to use pattern validator and define it as either or will pass.

ghiscoding added a commit that referenced this issue Dec 21, 2015
- Fixed issue #91 interpolation problem with remove validation doesn't
work twice.
- Enhancement #98 possibility to use one validation or another (tell how
many Validators are required for field to become valid).
- Enhancement #97 possibility to validate even on empty text field
(useful on `custom` and `remote` validation).
- Refined some Validators (IPV6 now include compressed/uncompressed
addresses, added more Polish characters to other Validators)
ghiscoding added a commit that referenced this issue Dec 21, 2015
@ghiscoding
Copy link
Owner

Ok so I made a change and added your request into the latest release v1.4.18

You can now use a new attribute named valid-require-how-many to simulate "this Validator OR that Validator". So for example, if you want to validate IPV4 or IPV6, you could then do this

<input type="text" 
       name="input2"
       ng-model="input2"
       validation="ipv4|ipv6"
       valid-require-how-many="1" />

But please note that if you include the required Validator, you will need to count +1 since you want (IPV4 or IPV6) and (required), so how many do we require for the field to be valid in that case? Answer is 2. Then if we take the same example but also include the required, it becomes this

<input type="text" 
       name="input2"
       ng-model="input2"
       validation="ipv4|ipv6|required"
       valid-require-how-many="2" />

You can see the new description inside the Wiki - Valid-Require-How-Many

I also made the IPV6 Validator a bit better by refining the Regex pattern expression, it can now accept both compressed (by removing leading 0) & uncompressed IPV6 addresses.

and don't forget to click on Star on top if you like and use the Angular-Validation package. Thanks :)

@pcxstephan
Copy link
Author

Great explanation and works like a charm! Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants