Gargoyle
(MM club member)
2007-03-31 01:30 AM
Input validation

 Code:
cls
?"input subnet to search?"
do
  gets $input
until $input <= 255


Seems simple enough, but how can one do the same basic idea but ensure that they did not enter any alpha characters?


Glenn BarnasAdministrator
(KiX Supporter)
2007-03-31 02:04 AM
Re: Input validation

Very old - might need some cleanup.. but you'll get the idea(s). Not NoVarsInStrings compliant, but I don't have access to the latest version (at work).

Glenn

 Code:
; get a string, according to defined rules
; Count specifies how many chars can be accepted
; Secure displays "*" instead of the chars typed
; Allowed is a string defining which chars will be accepted.
Function GetStr($Count, $Secure, OPTIONAL $Allowed)
 
  Dim $CC, $SF, $PC
 
  $CC = 0			; character count
  $GetStr = ""			; init return string
  $SF = 1			; string flag
  If $Allowed <> ""
    $Allowed = $Allowed + Chr(8) + Chr(13)
  EndIf
 
  While $SF = 1			; loop until done
    Get $PC			; get 1 character
 
    If $Allowed <> ""		; If allowed string is defined
      If InStr($Allowed, $PC) = 0 ;And ($PC <> Chr(13) or $PC <> Chr(8))
        $PC = ""		; char must be one of allowed
      EndIf
    EndIf
 
    Select
    Case $PC = Chr(8)		; handle backspace
      Chr(8) + " " + Chr(8)	; erase character
      $CC = $CC - 1		; reduce count
      If $CC > 0		; shorten or clear string
        $GetStr = Left($GetStr, $CC)
      Else
        $GetStr = ""
      EndIf
 
    Case $PC = Chr(13)		; handle Return
      $SF = 0
 
    Case $PC = ""		; handle non-allowed chars
      Beep
 
    Case 1			; got a char
      If $Secure
        "*"			; print "*" if secure
      Else
        "$PC"			; otherwise print the char
      EndIf
      $getStr = $GetStr + $PC	; add it to the string
      $CC = $CC + 1		; increment char count
    EndSelect
 
    ; Exit if character-count is reached
    If $Count > 0 and $CC = $Count
      $SF = 0
    EndIf
 
  Loop
EndFunction



AllenAdministrator
(KiX Supporter)
2007-03-31 02:12 AM
Re: Input validation

I've never used them, but Howard wrote some wrapper UDFs for RegExp which does exactly what you want. See this link:
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=2&Number=76617

I had my own way of doing it, but cannot seem to find it right now...


Gargoyle
(MM club member)
2007-03-31 02:17 AM
Re: Input validation

Thanks Glenn

Howard Bullock
(KiX Supporter)
2007-03-31 03:40 AM
Re: Input validation

Silver Platter:
 Code:
Break on
$RegExPattern = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
do
  ? "Enter IP Address: "
  gets $input
until RegExpTest($RegExPattern, $input, 1)

? "Valid IP address: " + $input


Function RegExpTest($Pattern, $String, $IgnoreCase)

   Dim $regEx, $Match, $Matches             ; Create variable.

   $regEx = createobject("VBscript.RegExp") ; Create a regular expression.

   $regEx.Pattern    = $Pattern             ; Set pattern.
   $regEx.IgnoreCase = val($IgnoreCase)     ; Set case insensitivity.

   $RegExpTest = $regEx.Test($String)       ; Execute test search.
EndFunction



Gargoyle
(MM club member)
2007-03-31 04:01 PM
Re: Input validation

So if I only need to check on one of the octets

$RegExPattern = "^\d{1,3}$"

The first two are supplied and the fourth is not needed in this case (it is a ping sweep W/nslookup)


Witto
(MM club member)
2007-03-31 04:48 PM
Re: Input validation

In reaction to the first post, I would think this code is better
 Code:
Dim $input
CLS
?"input subnet to search?"
Do
  Gets $input
Until 255 >= $input AND 0 <= $input

To get the right implicit conversion of $input


Mart
(KiX Supporter)
2007-04-01 02:24 PM
Re: Input validation

How about his?
Been using it for some time and it works great.

Advanced Scripting » IsNumeric RFC..


Les
(KiX Master)
2007-04-01 04:54 PM
Re: Input validation

While there are interesting academic contributions, I like Witto's KISS method.

Witto
(MM club member)
2007-04-02 07:16 AM
Re: Input validation

Thanks Les, it is always nice to get appreciated.
To avoid alphabetic input, I would even add a more explicit conversion:
 Code:
Dim $input
CLS
?"input subnet to search?"
Do
	Gets $input
Until 255 >= $input AND 0 <= $input AND $input = CStr(CInt($input))

Hey, that answers the initial question


Gargoyle
(MM club member)
2007-04-02 07:09 PM
Re: Input validation

And this is what is great about KiX, Simple task and many ways to accomplish the task.

Thanks all, as has been suggested I will KISS the issue.


Mart
(KiX Supporter)
2007-04-02 07:34 PM
Re: Input validation

 Originally Posted By: Gargoyle

....
I will KISS the issue.


Hmm.... Kinky