Page 1 of 1 1
Topic Options
#175120 - 2007-03-31 01:30 AM Input validation
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
 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?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#175121 - 2007-03-31 02:04 AM Re: Input validation [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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

_________________________
Actually I am a Rocket Scientist! \:D

Top
#175122 - 2007-03-31 02:12 AM Re: Input validation [Re: Gargoyle]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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...

Top
#175123 - 2007-03-31 02:17 AM Re: Input validation [Re: Glenn Barnas]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Thanks Glenn
_________________________
Today is the tomorrow you worried about yesterday.

Top
#175124 - 2007-03-31 03:40 AM Re: Input validation [Re: Gargoyle]
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
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



Edited by Howard Bullock (2007-03-31 03:43 AM)
Edit Reason: Made RegEx string better
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#175132 - 2007-03-31 04:01 PM Re: Input validation [Re: Howard Bullock]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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)
_________________________
Today is the tomorrow you worried about yesterday.

Top
#175133 - 2007-03-31 04:48 PM Re: Input validation [Re: Gargoyle]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
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

Top
#175138 - 2007-04-01 02:24 PM Re: Input validation [Re: Gargoyle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
How about his?
Been using it for some time and it works great.

Advanced Scripting » IsNumeric RFC..
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#175139 - 2007-04-01 04:54 PM Re: Input validation [Re: Mart]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
While there are interesting academic contributions, I like Witto's KISS method.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#175152 - 2007-04-02 07:16 AM Re: Input validation [Re: Les]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
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


Edited by Witto (2007-04-02 10:11 AM)

Top
#175178 - 2007-04-02 07:09 PM Re: Input validation [Re: Witto]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#175182 - 2007-04-02 07:34 PM Re: Input validation [Re: Gargoyle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
 Originally Posted By: Gargoyle

....
I will KISS the issue.


Hmm.... Kinky
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
2 registered (morganw, mole) and 414 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.062 seconds in which 0.022 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org