在Angular中验证IPv6地址

时间:2017-07-14 08:29:52

标签: javascript angularjs regex validation

在Angular中,我有许多需要IPv6地址的字段,这些字段需要进行验证,例如:

<input id                = "domain-IPv6_network" 
       type              = "text" 
       name              = "IPv6_network" 
       ng-model          = "network.IPv6_network"
       ng-pattern        = "ipv6_pattern"/>

ipv6_pattern是我在其他地方定义的模式。它可以在这个网站上找到的IPv6模式作为许多其他问题的答案,要求使用javascript正则表达式IPv6模式:

$scope.ipv6_pattern = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\:){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/

这在大多数情况下都可以正常工作。但是,当我针对字符串测试时:

9:9:9:9

该模式也匹配此字符串。我的问题是:这是一个有效的IPv6地址吗?如果没有,那么当输入这样的字符串时,验证IPv6地址的正确的正则表达式是什么?

1 个答案:

答案 0 :(得分:1)

public static long getDefaultCompanyId(){
        long companyId = 0;
        try{ companyId = getDefaultCompany().getCompanyId(); }
        catch(Exception e){ System.out.println(e.getClass() + " " +e.getMessage()); }
       return companyId;
}

public static long getDefaultGroupId (){

    long companyId = getDefaultCompanyId();
    long globalGroupId = 0L;

    Group group = null;
    try {
        group = GroupLocalServiceUtil.getGroup(companyId, "Guest");
    } catch (PortalException | SystemException e) {
        e.printStackTrace();
        return globalGroupId;
    }
     globalGroupId = group.getGroupId();


    return globalGroupId;
}

使用以下正则表达式检查有效的IPV6地址

9:9:9:9 is invalid IPV6 address.