edumanilha Posted February 26, 2020 Share Posted February 26, 2020 (edited) while 1 Global $destino = ControlGetText("GUI2", "", $Final) $IPdestino = StringSplit($destino,".") ;MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!",$destino & $IPdestino[1] & $IPdestino[2] & $IPdestino[3] & $IPdestino[4],10) If $IPdestino[0] Or $IPdestino[1] Or $IPdestino[2] Or $IPdestino[3] = "" then MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!","one two or three are empty",10) ElseIf $IPdestino[4] <> "" Then MsgBox($MB_SYSTEMMODAL, "CAMPO EM BRANCO!!!","4 filled",10) If $IPdestino[2] + $IPdestino[3] <> $Filial Then $Filial = $IPdestino[2]+$IPdestino[3] GUICtrlSetData($labelfilialdest,"Filial: " & $Filial) EndIf EndIf wend Hello, I have this code, the first part get text from a input, then split the result (IP adress), and then I tried to filter the actions, to do nothing on blank results and do something when the 4 octets are filled... But I cant entrer the IF's... I don't know why...Maybe something silly because I spent all day long playing with codes... I put the msgbox for diagnosis, and I can see until [4] have values...I entered the [1],[2],[3] blank check part even with they filled and the [4] check I can't arrive.... Speaking about Ip adresses...Someone have a nice IP check filter? only numbers, ony full filled etc etc... ? Thanks in advance! Edited February 26, 2020 by edumanilha Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted February 26, 2020 Share Posted February 26, 2020 (edited) @edumanilha Each of the condition in your If must be compared to something, and now you are just comparing the third element of the $IPdestino array with a blank value. If $IPdestino[0] = "" Or $IPdestino[1] = "" Or $IPdestino[2] = "" Or $IPdestino[3] = "" Then ; Statements Else ; Other statements EndIf Or, using a Loop: For $i = 0 To UBound($IPdestino) - 1 Step 1 If $IPdestino[$i] == "" Then ConsoleWrite("Field " & $i & " is blank!" & @CRLF) Exit For EndIf Next 3 hours ago, edumanilha said: Speaking about Ip adresses...Someone have a nice IP check filter? only numbers, ony full filled etc etc... ? Regular Expressions is the way to do it (not the only one, but the most efficent) Edited February 26, 2020 by FrancescoDiMuro Musashi and edumanilha 2 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Musashi Posted February 26, 2020 Share Posted February 26, 2020 (edited) A simple search will return various examples for an IP filter, some with RegEx and some without. Here is an example without RegEx (Thanks goes to @BrewManNH ) : https://www.autoitscript.com/wiki/Snippets_(_Internet_)#ValidIP Spoiler expandcollapse popup#include <Array.au3> ; This is only needed for the _ArrayDisplay function used in the example below, and is not needed for the _ValidIP function ; IPv4 validation script Global Const $IPAddress = "192.168.2.1" Global Const $Return = _ValidIP($IPAddress) If $Return < 0 Then Switch @error Case 1 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239") Case 2 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters") Case 3 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)") Case 4 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 4 - Last octet is either 0 or 255") EndSwitch Else MsgBox(48, "", $IPAddress & " is a valid Class " & $Return[5] & " IP address") _ArrayDisplay($Return) EndIf ; FUNCTION# =========================================================================================================== ; Name...........: _ValidIP ; Description ...: Verifies whether an IP address is a valid IPv4 address or not ; Syntax.........: _ValidIP($sIP) ; Parameters ....: $sIP - IP address to validate ; ; Return values .: Success - Array containing split IP Address, IP address in Hex, and the Class of the IP address ; array[0] - [3] = the IP address split into octets ; array[4] = IP address in Hex ; array[5] = Class of the IP address [A through D] ; Failure - -1, sets @error ; |1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239 ; |2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters ; |3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20) ; |4 - Last octet ends in 0 or 255 which are invalid for an IP address ; Author ........: BrewManNH ; Modified.......: ; Remarks .......: This will accept an IP address that is 4 octets long, and contains only numbers and falls within ; valid IP address values. Class A networks can't start with 0 or 127. 169.xx.xx.xx is reserved and is ; invalid and any address that starts above 239, ex. 240.xx.xx.xx is reserved. The address range ; 224-239 1s reserved as well for Multicast groups but can be a valid IP address range if you're using ; it as such. Any IP address ending in 0 or 255 is also invalid for an IP ; Related .......: ; Link ..........: ; Example .......: Yes ; ===================================================================================================================== Func _ValidIP($sIP) Local $adIPAddressInfo[6] Local $aArray = StringSplit($sIP, ".", 2) If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1) Local $dString = "0x" If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then Return SetError(1, 0, -1) EndIf For $I = 0 To 3 If $I < 3 Then If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then Return SetError(2, 0, -1) EndIf Else If Not StringIsDigit($aArray[$I]) Then Return SetError(2, 0, -1) EndIf If $aArray[$I] < 1 Or $aArray[$I] > 254 Then Return SetError(4, 0, -1) EndIf EndIf $dString &= StringRight(Hex($aArray[$I]), 2) $adIPAddressInfo[$I] = $aArray[$I] Next $adIPAddressInfo[4] = $dString Switch $aArray[0] Case 1 To 126 $adIPAddressInfo[5] = "A" Return $adIPAddressInfo Case 128 To 191 $adIPAddressInfo[5] = "B" Return $adIPAddressInfo Case 192 To 223 $adIPAddressInfo[5] = "C" Return $adIPAddressInfo Case 224 To 239 $adIPAddressInfo[5] = "D" Return $adIPAddressInfo EndSwitch EndFunc ;==>_ValidIP The code is a bit longer, but easier to follow (especially for beginners). You may e.g. also take a look at the thread : regular-expression-to-confirm-an-ipv4-address Edited February 26, 2020 by Musashi FrancescoDiMuro and edumanilha 2 "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
edumanilha Posted February 26, 2020 Author Share Posted February 26, 2020 5 hours ago, FrancescoDiMuro said: @edumanilha Each of the condition in your If must be compared to something, and now you are just comparing the third element of the $IPdestino array with a blank value. If $IPdestino[0] = "" Or $IPdestino[1] = "" Or $IPdestino[2] = "" Or $IPdestino[3] = "" Then ; Statements Else ; Other statements EndIf Or, using a Loop: For $i = 0 To UBound($IPdestino) - 1 Step 1 If $IPdestino[$i] == "" Then ConsoleWrite("Field " & $i & " is blank!" & @CRLF) Exit For EndIf Next Regular Expressions is the way to do it (not the only one, but the most efficent) I knew it something silly...You know when you spend all day long programming and at some point you don't know what you,re doing anymore? I looked many times and forgot about each Or comparator... Thanks! Musashi 1 Link to comment Share on other sites More sharing options...
edumanilha Posted February 26, 2020 Author Share Posted February 26, 2020 8 hours ago, Musashi said: A simple search will return various examples for an IP filter, some with RegEx and some without. Here is an example without RegEx (Thanks goes to @BrewManNH ) : https://www.autoitscript.com/wiki/Snippets_(_Internet_)#ValidIP Reveal hidden contents expandcollapse popup#include <Array.au3> ; This is only needed for the _ArrayDisplay function used in the example below, and is not needed for the _ValidIP function ; IPv4 validation script Global Const $IPAddress = "192.168.2.1" Global Const $Return = _ValidIP($IPAddress) If $Return < 0 Then Switch @error Case 1 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239") Case 2 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters") Case 3 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)") Case 4 MsgBox(64, "", "Error in IP address: " & $IPAddress & @LF & "Error code is: 4 - Last octet is either 0 or 255") EndSwitch Else MsgBox(48, "", $IPAddress & " is a valid Class " & $Return[5] & " IP address") _ArrayDisplay($Return) EndIf ; FUNCTION# =========================================================================================================== ; Name...........: _ValidIP ; Description ...: Verifies whether an IP address is a valid IPv4 address or not ; Syntax.........: _ValidIP($sIP) ; Parameters ....: $sIP - IP address to validate ; ; Return values .: Success - Array containing split IP Address, IP address in Hex, and the Class of the IP address ; array[0] - [3] = the IP address split into octets ; array[4] = IP address in Hex ; array[5] = Class of the IP address [A through D] ; Failure - -1, sets @error ; |1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239 ; |2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters ; |3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20) ; |4 - Last octet ends in 0 or 255 which are invalid for an IP address ; Author ........: BrewManNH ; Modified.......: ; Remarks .......: This will accept an IP address that is 4 octets long, and contains only numbers and falls within ; valid IP address values. Class A networks can't start with 0 or 127. 169.xx.xx.xx is reserved and is ; invalid and any address that starts above 239, ex. 240.xx.xx.xx is reserved. The address range ; 224-239 1s reserved as well for Multicast groups but can be a valid IP address range if you're using ; it as such. Any IP address ending in 0 or 255 is also invalid for an IP ; Related .......: ; Link ..........: ; Example .......: Yes ; ===================================================================================================================== Func _ValidIP($sIP) Local $adIPAddressInfo[6] Local $aArray = StringSplit($sIP, ".", 2) If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1) Local $dString = "0x" If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then Return SetError(1, 0, -1) EndIf For $I = 0 To 3 If $I < 3 Then If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then Return SetError(2, 0, -1) EndIf Else If Not StringIsDigit($aArray[$I]) Then Return SetError(2, 0, -1) EndIf If $aArray[$I] < 1 Or $aArray[$I] > 254 Then Return SetError(4, 0, -1) EndIf EndIf $dString &= StringRight(Hex($aArray[$I]), 2) $adIPAddressInfo[$I] = $aArray[$I] Next $adIPAddressInfo[4] = $dString Switch $aArray[0] Case 1 To 126 $adIPAddressInfo[5] = "A" Return $adIPAddressInfo Case 128 To 191 $adIPAddressInfo[5] = "B" Return $adIPAddressInfo Case 192 To 223 $adIPAddressInfo[5] = "C" Return $adIPAddressInfo Case 224 To 239 $adIPAddressInfo[5] = "D" Return $adIPAddressInfo EndSwitch EndFunc ;==>_ValidIP The code is a bit longer, but easier to follow (especially for beginners). You may e.g. also take a look at the thread : regular-expression-to-confirm-an-ipv4-address I just thought I could seize the oportunity , since I'm talking abou IP's...But thanks for your examples! I'll try! Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted February 27, 2020 Share Posted February 27, 2020 12 hours ago, edumanilha said: silly...You know when you spend all day long programming and at some point you don't know what you,re doing anymore? Definitely yes, and it is very annoying! Sometimes it is better to stop working when it's late, and begin the next day with a bunch of hours of sleep. By the way, happy to have helped edumanilha 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now