ElPandaRojo1 Posted April 7, 2020 Share Posted April 7, 2020 I need help, how can I find out if the information they give me is a number. Example I have the InputBox give me any number as I do to know if in a number, instead of it being some letter or some word Help.au3 Link to comment Share on other sites More sharing options...
water Posted April 7, 2020 Share Posted April 7, 2020 IsNumber? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
TheXman Posted April 7, 2020 Share Posted April 7, 2020 (edited) One way could be to create a function that tests the value. The example below assumes that floating point values are assumed to be numeric. If you only want integers, then you can modify as needed. ConsoleWrite('IsNumeric(0) = ' & IsNumeric(0) & @CRLF) ConsoleWrite('IsNumeric(798) = ' & IsNumeric(798) & @CRLF) ConsoleWrite('IsNumeric("") = ' & IsNumeric("") & @CRLF) ConsoleWrite('IsNumeric("7 ") = ' & IsNumeric("7 ") & @CRLF) ConsoleWrite('IsNumeric("7") = ' & IsNumeric("7") & @CRLF) ConsoleWrite('IsNumeric("7a") = ' & IsNumeric("7a") & @CRLF) ConsoleWrite('IsNumeric("1,234.00") = ' & IsNumeric("1,234.00") & @CRLF) ConsoleWrite('IsNumeric("1234.00") = ' & IsNumeric("1234.00") & @CRLF) ConsoleWrite('IsNumeric("1234.00.23") = ' & IsNumeric("1234.00.23") & @CRLF) ConsoleWrite('IsNumeric("1234.") = ' & IsNumeric("1234.") & @CRLF) Func IsNumeric($sValue) If StringRegExp($sValue, "^\d+(\.\d*)?$") Then Return True Else Return False EndIf EndFunc Output: IsNumeric(0) = True IsNumeric(798) = True IsNumeric("") = False IsNumeric("7 ") = False IsNumeric("7") = True IsNumeric("7a") = False IsNumeric("1,234.00") = False IsNumeric("1234.00") = True IsNumeric("1234.00.23") = False IsNumeric("1234.") = True $sInput = InputBox("Test", "Enter data") If IsNumeric($sInput) Then MsgBox(0, "Test", $sInput & " is numeric") Else MsgBox(0, "Test", $sInput & " is not numeric") EndIf Func IsNumeric($sValue) If StringRegExp($sValue, "^\d+(\.\d*)?$") Then Return True EndFunc Edited April 7, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
TheXman Posted April 7, 2020 Share Posted April 7, 2020 (edited) 13 minutes ago, water said: IsNumber? @water I don't think IsNumber will work in this case. IsNumber tells you if the variable's base type is a number. InputBox returns a string. So IsNumber will always be False. Edited April 7, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
ElPandaRojo1 Posted April 7, 2020 Author Share Posted April 7, 2020 Thank you so much @water for your answer, but @TheXman is right in what he says. Thank you so much @TheXman your function serves me. Link to comment Share on other sites More sharing options...
water Posted April 7, 2020 Share Posted April 7, 2020 Based on your example I suggest the following (non-RegExp) solution: $sInput = InputBox("Test", "Enter data") ; Returns input as a string $vInput = Number($sInput) ; Return the numeric representation of the input. Means: Drop all non-numeric characters If String($vInput) == $sInput Then ; Translate the number to a string and compare with the original input. When not equal then the input is not numeric MsgBox(0, "Test", $sInput & " is a number") Else MsgBox(0, "Test", $sInput & " contains non-numeric characters") EndIf My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
jchd Posted April 7, 2020 Share Posted April 7, 2020 (edited) Beware of "003" != "3" as well as "5e-1" != "0.5" as well as "+8" != "8" as well as "-0" != "0" or some "0.0" != "0" and "1.20" != "1.2" Users can be either vicious or surprising. Edited April 7, 2020 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
water Posted April 7, 2020 Share Posted April 7, 2020 Seems we need more information about the range of valid input My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
dmob Posted April 7, 2020 Share Posted April 7, 2020 14 minutes ago, jchd said: Users can be either vicious or surprising. I found out in very unpleasant ways Einstein's quote came alive Link to comment Share on other sites More sharing options...
BrewManNH Posted April 7, 2020 Share Posted April 7, 2020 I'd suggest using a GUI and an input control created with GUICtrlCreateInput, then you can specify that it would only accept numbers with the style $ES_NUMBER set. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 7, 2020 Share Posted April 7, 2020 (edited) $sRet = InputBox('Number', 'Enter number') If Not @error Then MsgBox(64, @ScriptName, 'Is Number: ' & _StringIsNumber($sRet)) EndIf Func _StringIsNumber($sString, $bFloat = True) Return StringIsDigit($sString) Or StringIsInt($sString) Or ($bFloat And StringIsFloat($sString)) ;Or as suggested by water ;Return String($sString) == Number($sString) EndFunc Edited April 7, 2020 by MrCreatoR water 1 Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Zedna Posted April 8, 2020 Share Posted April 8, 2020 There are function that can be used in this case: StringIsDigit() StringIsFloat() Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
water Posted April 8, 2020 Share Posted April 8, 2020 Already used in the example by MrCreatoR in the above post My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted April 8, 2020 Share Posted April 8, 2020 (edited) Seems that the closest one was by @TheXman, since no one of the above works with a floating number with a comma instead of a dot as decimal separator.One flavour could be something like this: #include <MsgBoxConstants.au3> #include <StringConstants.au3> Global $strUserInput = InputBox("_IsNumber?", "Type whatever you want: ") If Not @error Then MsgBox($MB_ICONQUESTION, "The answer:", "Is " & $strUserInput & " a number? " & _IsNumber($strUserInput)) EndIf Func _IsNumber($varUserInput) Return StringRegExp($varUserInput, '^(?:[-+])?\d+(?:[.,]\d*)?(?:[.,]\d*)?$', $STR_REGEXPMATCH) = 1 EndFunc It supports number with sign, decimal separator and thousand separator as dot or comma P.S.: As @jchd said, the cases of what should be considered as number are a lot Numbers format supported: Spoiler +000.5 -01.16 +5 -10 15,1 15.5 +20.2 -100,5 1.000,4 1,000.5 +50.000,56 -12,12345.00 Edited April 8, 2020 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Colduction Posted April 8, 2020 Share Posted April 8, 2020 You can also use this alternative method: $sText = "Example Text 123" If StringRegExp($sText, "^[0-9]") Then ConsoleWrite("Yes!" & @CRLF) Else ConsoleWrite("No, it's not digit!" & @CRLF) EndIf Link to comment Share on other sites More sharing options...
jchd Posted April 8, 2020 Share Posted April 8, 2020 (edited) Too naive as other posts above showed. Consider: +5 -1 33333333333333333333333333333333333333333 1.0 1.1 and many more. The OP needs to be more precise about what he can allow: floating-point or integer, sign, range. Edited April 8, 2020 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 8, 2020 Share Posted April 8, 2020 (edited) 3 hours ago, FrancescoDiMuro said: no one of the above works with a floating number with a comma instead of a dot as decimal separator It's not a number then. Floating number separated with dot, not with comma, otherwise it's a string. You (as a programmer) should define the format for the input. If it's not in defined format, it's considered wrong (unsupported) input. Edited April 8, 2020 by MrCreatoR FrancescoDiMuro 1 Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now