|
|
|||||||
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? |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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... |
||||||||
|
|
|||||||
Thanks Glenn |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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) |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
How about his? Been using it for some time and it works great. Advanced Scripting » IsNumeric RFC.. |
||||||||
|
|
|||||||
While there are interesting academic contributions, I like Witto's KISS method. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
Originally Posted By: Gargoyle .... I will KISS the issue. Hmm.... Kinky |