> Op 27 oktober 2016 om 7:52 schreef Marc-Aurèle Brothier <ma...@exoscale.ch>:
> 
> 
> From a quick look at the code you've forgotten one "|" for the or condition:
> 

No, I didn't. Otherwise the test will always fail since either IPv4 or IPv6 
will fail.

I think I got it now:

$.validator.addMethod("ipv46cidr", function(value, element) {
    if (this.optional(element) && value.length == 0)
        return true;

    var parts = value.split('/');
    if (typeof parts == 'undefined' || parts.length != 2) {
        return false;
    }

    if (parts[1] != Number(parts[1]).toString())
        return false;

    if (Number(parts[1]) < 0)
        return false;

    if ($.validator.methods.ipv4.call(this, parts[0], element)) {
        if (Number(parts[1] > 32))
            return false;

        return true;
    } else if ($.validator.methods.ipv6.call(this, parts[0], element)) {
        if (Number(parts[1] > 128))
            return false;

        return true;
    } else if (parts[0] == '::') {
        if (Number(parts[1] > 128))
            return false;

        return true;        
    }

    return false;
}, "The specified IPv4/IPv6 CIDR is invalid.");

Wido

> $.validator.addMethod("ipv46cidr", function(value, element) {
>     if (!$.validator.methods.ipv4cidr.call(this, value, element) ||
> !$.validator.methods.ipv6cidr.call(this, value, element))
>         return false;
> 
>     return true;
> }, "The specified IPv4/IPv6 CIDR is invalid.");
> 
> On Wed, Oct 26, 2016 at 4:37 PM, Wido den Hollander <w...@widodh.nl> wrote:
> 
> > So my JS skills are way to low, but I tried this, but it doesn't seem to
> > work:
> >
> > $.validator.addMethod("ipv46cidr", function(value, element) {
> >     if (!$.validator.methods.ipv4cidr.call(this, value, element) |
> > !$.validator.methods.ipv6cidr.call(this, value, element))
> >         return false;
> >
> >     return true;
> > }, "The specified IPv4/IPv6 CIDR is invalid.");
> >
> > What am I missing here?
> >
> > Wido
> >
> > > Op 23 oktober 2016 om 9:37 schreef Rohit Yadav <
> > rohit.ya...@shapeblue.com>:
> > >
> > >
> > > Hi Wido,
> > >
> > >
> > > Yes, you can add a new validator that can validator that the provided
> > address is either ipv4 or ipv6, here:
> > >
> > > https://github.com/apache/cloudstack/blob/master/ui/
> > scripts/sharedFunctions.js#L2327
> > >
> > >
> > > Give the validator any appropriate name, and use it in the network.js
> > code replacing the currently defined validator with yours.
> > >
> > >
> > > Regards.
> > >
> > > ________________________________
> > > From: Wido den Hollander <w...@widodh.nl>
> > > Sent: 21 October 2016 17:33:29
> > > To: dev@cloudstack.apache.org
> > > Subject: Question about JavaScript validators in UI
> > >
> > > Hi,
> > >
> > > While working on the IPv6 for Basic Networking I'm at the stage of the
> > Security Groups.
> > >
> > > When entering a CIDR in the UI which is not IPv4 (eg ::/0) it will show:
> > 'The specified IPv4 CIDR is invalid.'
> > >
> > > That's true, so looking in network.js I see this piece of code:
> > >
> > > 'cidr': {
> > >   edit: true,
> > >   label: 'label.cidr',
> > >   isHidden: true,
> > >   validation: {
> > >     ipv4cidr: true
> > >   }
> > >  },
> > >
> > > There is a ipv6cidr validation method as well. How can I modify the
> > JavaScript in such a way that either a valid IPv4 OR IPv6 CIDR has to be
> > entered?
> > >
> > > My JavaScript skills are rather low.
> > >
> > > Thanks!
> > >
> > > Wido
> > >
> > > rohit.ya...@shapeblue.com
> > > www.shapeblue.com
> > > 53 Chandos Place, Covent Garden, London  WC2N 4HSUK
> > > @shapeblue
> > >
> > >
> > >
> >

Reply via email to