Sign in to follow this  
Followers 0
buymeapc

Input Box Data Validation

7 posts in this topic

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.

Share this post


Link to post
Share on other sites



maybe

$x = Number("3.14") ;returns 3.14

..... so

$x = number($inpuT)

if $x = $input then ; its a number

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by buymeapc

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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! :lmao:

Share this post


Link to post
Share on other sites

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 :o

(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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0