buymeapc Posted February 6, 2006 Share Posted February 6, 2006 I have a GUI which prompts for an IP address. Is there a way to validate that the user input the correct info? (Ex. 192.168.0.1) I basically want to validate that there are no alphabetic characters. Link to comment Share on other sites More sharing options...
Valuater Posted February 6, 2006 Share Posted February 6, 2006 maybe $x = Number("3.14") ;returns 3.14 ..... so $x = number($inpuT) if $x = $input then ; its a number 8) Link to comment Share on other sites More sharing options...
buymeapc Posted February 6, 2006 Author Share Posted February 6, 2006 (edited) This is what I tried: $q = Number(GUICtrlRead($rechip)) If Not $q = GUICtrlRead($rechip) Then MsgBox(0, "", "IP is not correct.") GUICtrlSetState($rechip,$GUI_FOCUS) Call ("GUI_Start") EndIf And it just is not working...Am I doing something wrong? Edited February 6, 2006 by buymeapc Link to comment Share on other sites More sharing options...
Dickb Posted February 6, 2006 Share Posted February 6, 2006 I have a GUI which prompts for an IP address. Is there a way to validate that the user input the correct info? (Ex. 192.168.0.1) I basically want to validate that there are no alphabetic characters. Example: Opt("MustDeclareVars", 1) Main() Exit Func Main() Local $IP Do $IP = InputBox("IPAddress", "Enter IP", "", "", 100, 120) If @error Then Exit Until CheckIP($IP) MsgBox(0, "IP", $IP) EndFunc Func CheckIP(ByRef $IP) Local $Ret, $aIP $Ret = True $aIP = StringSplit($IP, ".") If $aIP[0] <> 4 Then; The must be 4 octets $Ret = False Else For $i = 1 to 4 If Number($aIP[$i]) < 0 Or Number($aIP[$i]) > 255 Then $Ret = False; Must be 0 to 255 Next If $IP = "0.0.0.0" Or $IP = "255.255.255.255" Then $Ret = False; Not proper IP addresses Else $IP = StringFormat("%d.%d.%d.%d", $aIP[1], $aIP[2], $aIP[3], $aIP[4]); Strip leading zeroes EndIf EndIf Return $Ret EndFunc Link to comment Share on other sites More sharing options...
jonk Posted February 6, 2006 Share Posted February 6, 2006 a regexp to determine a valid ip: StringRegExp($s_ip, "(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]?)\.(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]?)") this Code is out of the Function _INetSmtpMail() which could be find in the "inet.au3" include. Link to comment Share on other sites More sharing options...
buymeapc Posted February 6, 2006 Author Share Posted February 6, 2006 Here's the solution that helped me: $aIP = StringSplit(GUICtrlRead($rechip), ".") If $aIP[0] <> 4 Then MsgBox(48, "", "IP address is an incorrect length.") GUICtrlSetState($rechip,$GUI_FOCUS) Call ("GUI_Start") Else For $i = 1 to 4 If Not StringIsDigit($aIP[$i]) Then MsgBox(48, "", $aIP[$i]&" is not a digit.") GUICtrlSetState($rechip,$GUI_FOCUS) Call ("GUI_Start") EndIf If Number($aIP[$i]) < 0 Or Number($aIP[$i]) > 255 Then MsgBox(48, "", "Octet "&$aIP[$i]&" is not correct.") GUICtrlSetState($rechip,$GUI_FOCUS) Call ("GUI_Start") EndIf Next If GUICtrlRead($rechip) = "0.0.0.0" Or GUICtrlRead($rechip) = "255.255.255.255" Then MsgBox(48, "", "IP is not valid.") GUICtrlSetState($rechip,$GUI_FOCUS) Call ("GUI_Start") EndIf EndIf Thanks very much for all the help! Link to comment Share on other sites More sharing options...
foggw Posted February 11, 2006 Share Posted February 11, 2006 a regexp to determine a valid ip: StringRegExp($s_ip, "(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]?)\.(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]?)") this Code is out of the Function _INetSmtpMail() which could be find in the "inet.au3" include. @jonk Thanks! I am trying to learn Regular Expressions so I took the example and added some input and output. But it turns out that according to the test pattern above that $s_ip = "LionsAndTigersAndBears255.255.255.2599999999999" is a valid IP address (I noticed that 256.255.255.256 was being reported as being valid and then I got rediculous) I finally figured out that the two values in the center must be bracketed by periods. But, only the end of the first value and the beginning of the last value are checked. One way to correct this would be to add delimiters to both ends of the IP address $s_ip and test it with a pattern including the delimiters. ;Original code StringRegExp($s_ip, "(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]?)\.(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]?)") ;Modified code StringRegExp("|" & $s_ip & "|","\|(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]?)\.(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]?)\|") \| has been added to the beginning and end of the StringRegExp pattern. Bill 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