function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mrunali GaonkarMrunali Gaonkar 

Validation for IPV4 and IPV6 Input

I am working on validation for IPV4 and IPV6 to validate their input.
This IP input fields should also accept the subnet mask.

Valid input in IPV4 field can be :
172.16.0.0
172.16.0.0/8
172.16.0.0/192
213.248.89.98/31

Valid input in IPV6 field can be :
2001:db8:abcd:0012::0/64
2001:db8:abcd:0012:0000:0000:0000:0000
2001:2000:3080:001D::2/126

Is it something possible with regex or custom code? I tried couple of regex solutions but its not working. 
Best Answer chosen by Mrunali Gaonkar
Alain CabonAlain Cabon
The more difficult is to validate these complicated regexes with many tests.

only IPV6:   http://regexlib.com/REDetails.aspx?regexp_id=1000
^((([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})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$

Matches full and compressed IPv6 addresses as defined in RFC 2373http://www.faqs.org/rfcs/rfc2373.html ). No useful captures. Various implementations require different terminators. (i.e. ^-$ or \b-\b)

What is your RFC number for IPV4 and IPV6 ?

All Answers

Alain CabonAlain Cabon
@Mrunali Gaonkar,

http://regexlib.com/Search.aspx?k=ipv4

http://regexlib.com/Search.aspx?k=ipv6

When you have found a regex that works fine in the list above, just double the antislashes

For example:   http://regexlib.com/REDetails.aspx?regexp_id=2866

Title:  TitleTestFindIPV4 and IPV6 address

By just doubling the antislashes (\) that should be often sufficient for apex:
Pattern r = Pattern.compile('^(((([1]?\\d)?\\d|2[0-4]\\d|25[0-5])\\.){3}(([1]?\\d)?\\d|2[0-4]\\d|25[0-5]))|([\\da-fA-F]{1,4}(\\:[\\da-fA-F]{1,4}){7})|(([\\da-fA-F]{1,4}:){0,5}::([\\da-fA-F]{1,4}:){0,5}[\\da-fA-F]{1,4})$');
Matcher rm = r.matcher('2001:db8:abcd:0012:0000:0000:0000:0000'); 
if(rm.matches()) {
     system.debug('regex matches !'); 
} else {
    system.debug('regex doesnt match ...');   
}

The more difficult is to validate these complicated regexes with many tests.
 
Mrunali GaonkarMrunali Gaonkar
Hi Alain,

Thank you for the reply.
Its not working for IP with subnet mask.
for e.g 2001:db8:abcd:0012:0000:0000::0000/32
Alain CabonAlain Cabon
The more difficult is to validate these complicated regexes with many tests.

only IPV6:   http://regexlib.com/REDetails.aspx?regexp_id=1000
^((([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})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$

Matches full and compressed IPv6 addresses as defined in RFC 2373http://www.faqs.org/rfcs/rfc2373.html ). No useful captures. Various implementations require different terminators. (i.e. ^-$ or \b-\b)

What is your RFC number for IPV4 and IPV6 ?
This was selected as the best answer
Mrunali GaonkarMrunali Gaonkar
I dont know about RFC. I will confirm and will let you know.
I tried given regex in the validation. I dont what exactly is the problem but its giving a syntax error. Below is the validation rule condition formula.

NOT(REGEX(IPv6_Address__c ,"^((([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})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$" ))
Alain CabonAlain Cabon
Why don't you double the backslashes?

I created the same formula without any problem.

By just doubling the antislashes (\) of the regexlib examples, that works for me.

You must escape the "\" with another "\" so the result is "\\" in Apex/Formulae.

Compare the example above: For example:   http://regexlib.com/REDetails.aspx?regexp_id=2866  and the same formula above in Apex.
Alain CabonAlain Cabon
backslashes ou antislashes ( \ ).
Mrunali GaonkarMrunali Gaonkar
Hi Alain,

Thank you so much for help. I appreciate it. I tried this regex and this are working fine as per my requirement.
a
IPV4 :
NOT(REGEX(IPv4_Address__c ,"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))?$" ))

IPV6 : 
NOT(REGEX(IPv6_Address__c, "((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))?)" ))