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
Naresh Krishna.ax1176Naresh Krishna.ax1176 

Validates the range between two ip addrresses?

Hi All,

 

I have to input two ip addresses from apex page. i.e., ipFrom and ipTo

for example:

ipFrom - 10.35.34.90

ipTo - 10.35.34.80

 

Above is incorrect range.

 

Is there any method to validate the ip address range as above ?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Assuming all IPs are formatted as 4-octet numbers, you can do this:

 

integer iptoint(string ip) {
string[] octet = ip.split('\\.');
return integer.valueof(octet[0])*16777216+integer.valueof(octet[1])*65536+integer.valueof(octet[2])*256+integer.valueof(octet[3]);
}

boolean isiniprange(string fromip, string toip, string currentip) {
integer lowip, highip, myip;
lowip = iptoint(fromip);
highip = iptoint(toip);
myip = iptoint(currentip);
return lowip <= myip && highip >= myip;

 

All Answers

sfdcfoxsfdcfox

Assuming all IPs are formatted as 4-octet numbers, you can do this:

 

integer iptoint(string ip) {
string[] octet = ip.split('\\.');
return integer.valueof(octet[0])*16777216+integer.valueof(octet[1])*65536+integer.valueof(octet[2])*256+integer.valueof(octet[3]);
}

boolean isiniprange(string fromip, string toip, string currentip) {
integer lowip, highip, myip;
lowip = iptoint(fromip);
highip = iptoint(toip);
myip = iptoint(currentip);
return lowip <= myip && highip >= myip;

 

This was selected as the best answer
Naresh Krishna.ax1176Naresh Krishna.ax1176

It worked. :) Thanks a lot