Code archives/Networking/IP Address validator (IPv4)
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
It will check an ip address is in the valid format so 127.0.0.1 will pass but 127.0.01. will fail. Its handy for making sure people entering IP Address into your games are entering something valid that the networking system can try to connect to. ** Use as you will, please credit me in the credits please ** :) | |||||
Function isIPAddressValid:Byte( IPAddress:String ) Local TempIP:String = IPAddress Local Occlet:String = "" Local OccletCount:Long = 0 Local OccletTest:Long = 0 For OccletTest = 1 To Len( IPAddress ) If Mid( IPAddress, OccletTest, 1 )>="0" And Mid( IPAddress, OccletTest, 1 )<="9" Or Mid( IPAddress, OccletTest, 1 )="." Else Return False EndIf Next Local DotString:String = IPAddress DotString = Replace( DotString, ".", "" ) 'remove the .'s there should only be three If Len( IPAddress )>Len( DotString )+3 Then Return False DotString = IPAddress DotString = Replace( DotString, "..", "" ) 'ipaddress dots should not be together If Len( IPAddress )<> Len( DotString ) Then Return False If Right( IPAddress, 1 ) = "." Then Return False If Left( IPAddress, 1 ) = "." Then Return False If Instr( IPAddress, "." ) = 0 Return False Else Occlet = Left( TempIP, Instr( TempIP, "." )-1 ) TempIP = Right( TempIP, Len( TempIP ) -Instr( TempIP, "." ) ) If ( Int( Occlet )<0 Or Int( Occlet )>255 ) Then Return False Else OccletCount :+ 1 If Instr( TempIP, "." ) = 0 Then Return False Occlet = Left( TempIP, Instr( TempIP, "." )-1 ) TempIP = Right( TempIP, Len( TempIP ) -Instr( TempIP, "." ) ) If ( Int( Occlet )<0 Or Int( Occlet )>255 ) Then Return False Else OccletCount :+ 1 If Instr( TempIP, "." ) = 0 Then Return False Occlet = Left( TempIP, Instr( TempIP, "." )-1 ) TempIP = Right( TempIP, Len( TempIP ) -Instr( TempIP, "." ) ) If ( Int( Occlet )<0 Or Int( Occlet )>255 ) Then Return False Else OccletCount :+ 1 If ( Int( TempIP )<0 Or Int( TempIP )>255 ) Then Return False Else OccletCount :+ 1 EndIf If OccletCount <4 Then Return False Return True End Function |
Comments
| ||
Function isIPAddressValid:Byte( IPAddress:String ) Try Ints. ;) ** Use as you will, please credit me in the credits please ** :) Public domain code... |
| ||
Public domain code... Hence the please, jeez everyones a critic ;) As for ints seeing as True and False are never greater than a byte its daft to use Ints |
| ||
Similar idea but using split to make the code more concise. Normally something a regex could do easily.. there's probably a BruceyModule somewhere :)SuperStrict Local testIPs:String[] = ["1.2.3.4", "a.b.c.d", "244.234.256.1", "foo.2.3.4", "1.2.3", "00003", "127.0.01."] For Local testIP:String = EachIn testIPs Print testIP +" "+ isValidIPv4(testIP) Next End Function isValidIPv4:Int(ipv4:String) Local parts:String[] = ipv4.split(".") If parts.length <> 4 Then Return False ' invalid number of parts For Local part:String = EachIn parts ' check for non numeric content For Local i:Int = 1 To Len(part) Local ascii:Int = Asc(Mid(part,i,1)) If ascii < 48 Or ascii > 57 Then Return False ' invalid character. should be 0..9 Next If Len(part) < 1 Or Len(part) > 3 Then Return False ' invalid part length If Int(part) < 0 Or Int(part) > 255 Then Return False ' invalid part range Next Return True End Function |
Code Archives Forum