Click to See Complete Forum and Search --> : convert asp script to JS


catchup
11-05-2003, 01:58 PM
I was wondering if it was possible to do what the ASP does client side. I have 2 text boxes on the page that i am entering ip address ranges in like: 164.248.0.1 - 164.248.0.171
I need to conver the ips to a number so i can compare another convert numbered ip to see if it is in that particular range. The ASP below converts the dotted ip to a number:

ipno =Dot2LongIP("rangeBegin")
ipno2 =Dot2LongIP2("rangeEND")

function Dot2LongIP (DottedIP)
If DottedIP = "" Then
Dot2LongIP = 0
Else
For i = 1 To 4
pos = InStr(PrevPos + 1, DottedIP, ".", 1)
If i = 4 Then
pos = Len(DottedIP) + 1
End If
num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
PrevPos = pos
Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
Next
End If
End function

function Dot2LongIP2 (DottedIP2)
......
End function

// I'm hoping to cut n paste the range begin and range end into the rangeBegin and rangeEnd text fields and have the JS function convert the dotted ips to a number that i could use math operators like < and > to see if an ip is in its range, ofcourse i'm using a database and blah blah...

Thanks

AdamBrill
11-05-2003, 04:18 PM
Try this:<script type="text/javascript">
String.prototype.makelong = function(){
arr_split = this.split('.');
for(var x=0; x<arr_split.length; x++){
while(arr_split[x].length<3){
arr_split[x] = "0"+arr_split[x];
}
}
return parseInt(arr_split.join(""));
}
rangestart = "123.456.789.10";
rangeend = "123.456.789.100";
ip = "123.456.789.9";
if(ip.makelong() >= rangestart.makelong() && ip.makelong() <= rangeend.makelong()){
alert(ip + " is within that range!");
}
</script>

catchup
11-05-2003, 06:36 PM
Adam...it just hit me, the js script converts the ip on the fly, or a least can be used that way, so i don't have to change ip dotted address before the db input, case things now work with the record set. Is that right? to me the functions seems to break down the ip then puts it back together without the . and then compare.. is this correct?

AdamBrill
11-05-2003, 08:58 PM
Actually, what I did is took out the periods so that it can compare them as numbers. If needed, I could also write a function that would turn them back into the correct IP address... However, since you are using a database, I highly doubt that you would need to do anything in JS, since you should be able to do it in ASP before you write it into the database or right after you pull it out. Also, unless JS is absolutely needed, I would suggest that you don't use it, since 13% of internet users don't have JS enabled...