How to make If The input contains letters and not containing numbers do something please help...
Announcement
Collapse
No announcement yet.
How to make If The input contains letters and not containing numbers do something
Collapse
X
-
By using pattern-matching with the various character-classes (which are outlined here):
Here are the fundamental character-classes relevant to your query:
%a - This character-class represents all letters A-Z and a-z.
%A - This character-class represents the inverse (ie. all NON-letters).
%d - This character-class represents all digits 0-9.
%D - This character-class represents the inverse (ie. all NON-digits).
+ - This repetition-operator matches the preceding character-class one or more times.
* - This repetition-operator matches the preceding character-class zero or more times.
So, by employing the native Lua string.match() function, you can restrict an action by checking to ensure NO numbers are being returned from the Input Box. Like this:
Code:sReturn = Input.GetText("Input1"); if not(sReturn:match("%d")) then [I][COLOR=#008000] -- check to ensure that NO digits are being returned, then -- do something here[/COLOR][/I] Dialog.Message("Notice", "Input1 does NOT contain any digits."); end
Or, likewise you could restrict the action by checking whether any NON-letter characters are being returned. Like this:
Code:sReturn = Input.GetText("Input1"); if not (sReturn:match("%A")) then [I][COLOR=#008000]-- check to ensure that ONLY letters are being returned, then -- do something here[/COLOR][/I] Dialog.Message("Notice", "Input1 contains ONLY letters."); end
- Likes 2
-
EDIT,
And just an addendum to this issue. Depending on exactly what you want to exclude/include from the result, tweaks will be needed. For example, in the 2nd code sample above, you'd probably want to code in a check for 'blank strings', too. Play around with the various iterations available to you by studying the character-classes listed at the URL linked to above. You'll see that there are many variations producing slightly different results. Your final solution will be dependent on how precise you want the end result to be. (ie. Whether to include the restriction of control-characters, whether to include/exclude punctuation-characters, etc...)
- Likes 1
Comment
-
And,
I'll just throw out this one other 'slightly simpler' method for restricting an action if a number is detected in the Input Box.
Note: This uses the native Lua str:find() function which should NOT be confused for the AMS function of String.Find().The latter uses Title-case, whereas the native Lua function uses lower-case. You must use lower-case for this.
Code:[I][COLOR=#008000]-- [B]NOTE:[/B] In this example, the string.find() function uses lower-case (ie. str:find) -- This is a native LUA function (not AMS specific). -- So, if you change it to the AMS version using Title-case (ie. String.Find(), it will fail.[/COLOR][/I] sReturn = Input.GetText("Input1"); nReturn = sReturn:find('[%d]') [COLOR=#008000][I]-- Look for a number in the return-string to report its position[/I][/COLOR] if not nReturn then [I][COLOR=#008000]-- If NO number can be found, then permit an action ... -- do something here[/COLOR][/I] Dialog.Message("Notice", "No numbers detected"); end
Comment
-
Originally posted by BioHazard View PostBy using pattern-matching with the various character-classes (which are outlined here):
Here are the fundamental character-classes relevant to your query:
%a - This character-class represents all letters A-Z and a-z.
%A - This character-class represents the inverse (ie. all NON-letters).
%d - This character-class represents all digits 0-9.
%D - This character-class represents the inverse (ie. all NON-digits).
+ - This repetition-operator matches the preceding character-class one or more times.
* - This repetition-operator matches the preceding character-class zero or more times.
So, by employing the native Lua string.match() function, you can restrict an action by checking to ensure NO numbers are being returned from the Input Box. Like this:
Code:sReturn = Input.GetText("Input1"); if not(sReturn:match("%d")) then [I][COLOR=#008000] -- check to ensure that NO digits are being returned, then -- do something here[/COLOR][/I] Dialog.Message("Notice", "Input1 does NOT contain any digits."); end
Or, likewise you could restrict the action by checking whether any NON-letter characters are being returned. Like this:
Code:sReturn = Input.GetText("Input1"); if not (sReturn:match("%A")) then [I][COLOR=#008000]-- check to ensure that ONLY letters are being returned, then -- do something here[/COLOR][/I] Dialog.Message("Notice", "Input1 contains ONLY letters."); end
but please if you know how to cancel the next script , i will be happy
Ex.Code:sReturn = Input.GetText("Input3"); nReturn = sReturn:find('[%d]') -- Look for a number in the return-string to report its position if not nReturn then -- If NO number can be found, then permit an action ... Dialog.Message("Attention!", "You can't convert LETTERS, just Numbers", MB_OK, MB_ICONSTOP, MB_DEFBUTTON3); end a = Input.GetText("Input3"); x = a * 17.9063 Label.SetText("Label8", String.Mid(x, 1, 5).. " EGP")
Code:a = Input.GetText("Input3"); x = a * 17.9063 Label.SetText("Label8", String.Mid(x, 1, 5).. " EGP")
Comment
-
Hi Ulrich,
Ever present, lurking in the background, I see? Is nice to hear from you. Always enjoy seeing you jump into an AMS-specific thread (but wish we heard from you more often in this neck of the woods, these days).
LOL, yes. I do have a tendency to look straight past the obvious at times, don't I? I guess though, by looking into native Lua functions, the newcomers do have an opportunity to expand their knowledge a little bit. So, hopefully it's not all for naught.
...............
Originally posted by ItzMagdy View Post...please if you know how to cancel the next script , i will be happy ...
I mean to cancel that script
Code:a = Input.GetText("Input3"); x = a * 17.9063 Label.SetText("Label8", String.Mid(x, 1, 5).. " EGP")
I don't understand your question. You'll have to elaborate.
Comment
-
Originally posted by BioHazard View Post
Hi Ulrich,
Ever present, lurking in the background, I see? Is nice to hear from you. Always enjoy seeing you jump into an AMS-specific thread (but wish we heard from you more often in this neck of the woods, these days).
LOL, yes. I do have a tendency to look straight past the obvious at times, don't I? I guess though, by looking into native Lua functions, the newcomers do have an opportunity to expand their knowledge a little bit. So, hopefully it's not all for naught.
...............
@ItzMagdy,
I don't understand your question. You'll have to elaborate.
Comment
-
So, you mean something like this, then?
Code:sReturn = Input.GetText("Input1"); nReturn = sReturn:find('[%a]');[I][COLOR=#008000] -- Look for any letters in the return-string[/COLOR][/I] if nReturn then [I][COLOR=#008000] -- If letter found at any position in return-string, then advise ...[/COLOR][/I] Dialog.Message("Attention!", "You can't convert LETTERS, just Numbers", MB_OK, MB_ICONSTOP, MB_DEFBUTTON3); else if sReturn == "" then [I][COLOR=#008000]-- Check to ensure that Input Box has not been left empty[/COLOR][/I] Dialog.Message("Attention!", "Input can NOT be left blank."); else [I][COLOR=#008000]-- Otherwise permit the action[/COLOR][/I] sOutput = sReturn * 17.9063; Label.SetText("Label1", String.Mid(sOutput, 1, 5).. " EGP"); end end
- Likes 1
Comment
Comment