Validating number in edit field

Post your working scripts, libraries and tools for AHK v1.1 and older
shinymonky
Posts: 9
Joined: 15 Mar 2016, 10:19

Validating number in edit field

24 Jul 2016, 11:29

As a rookie, I was quite proud to have figured this out myself. This short code will check the text in the Edit control and make sure it's a number. Any other keypresses are ignored. It works by running a subroutine whenever any change is made to the Edit control text, checking the last character in that string, if it is not one of the accepted charactrs (numbers or decimal point) then a delete is sent. It's not perfect - the user can stil enter non-numerical characters by changing the insertion point with the arrow keys, but it's a start.

Gui, Add, Text,, Number
Gui, Add, Edit, r1 w150 gCheck vNumberentered
Gui, Add, Button, x30 y190 w60 h30 gOkay, Okay
Gui, Add, Button, x100 y190 w60 h30 gCancel, Cancel
Gui, Show,,
Return

Okay:
Gui, Submit, Hide
MsgBox, % Numberentered
Return
Cancel:
Gui, Submit, Hide
Return

Check:
GuiControlGet, var, , vNumberentered
EnsureNumber(var)
Return

EnsureNumber(var)
{
StringLen, L, var
T := "1.234567890"
C := SubStr(var, L, 1)
StringGetPos, I, T, %C%
IfEqual, I, -1
Send, {BS}
Return
}
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Validating number in edit field

24 Jul 2016, 12:09

you can simply use the Number option when adding the Edit control:
https://autohotkey.com/docs/commands/Gu ... s.htm#Edit

:)

Code: Select all

Gui, Add, Text,, Numbers only
Gui, Add, Edit, r1 w150 Number
Gui, Show,,
Return

shinymonky
Posts: 9
Joined: 15 Mar 2016, 10:19

Re: Validating number in edit field

24 Jul 2016, 14:37

Number option doesn't allow decimal points.
User avatar
boiler
Posts: 17072
Joined: 21 Dec 2014, 02:44

Re: Validating number in edit field

24 Jul 2016, 19:12

FYI - Your script doesn't work as posted. You need to remove the "v" from "vNumberentered" in your Check: subroutine, then it works again.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Validating number in edit field

25 Jul 2016, 05:49

Good job! There are some problems though, you can add as many decimal points as you like, and if you move the input marker, you can type anything, also, you can paste in stuff.

I wrote this function:
Spoiler
If anyone can "break" it or knows a better/easier method, let me know.
just me
Posts: 9487
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Validating number in edit field

26 Jul 2016, 02:54

Code: Select all

#NoEnv
Gui, Add, Text, , Number
Gui, Add, Edit, r1 w150 gCheck vNumberentered
Gui, Add, Button, x30 y190 w60 h30 gOkay, Okay
Gui, Add, Button, x100 y190 w60 h30 gCancel, Cancel
Gui, Show, , Test
Return

Okay:
Gui, Submit, NoHide
MsgBox, 0, You entered:, % Numberentered
Return

GuiClose:
Cancel:
ExitApp

Check:
GuiControlGet, Var, , Numberentered
If Var Is Not Number
   Send, {BS}
Return
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Validating number in edit field

26 Jul 2016, 05:59

just me wrote:

Code: Select all

Check:
GuiControlGet, Var, , Numberentered
If Var Is Not Number
   Send, {BS}
Return
Perfect, should be marked as solved. Easy to add space block if needed.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Validating number in edit field

26 Jul 2016, 06:10

just me wrote:

Code: Select all

Check:
GuiControlGet, Var, , Numberentered
If Var Is Not Number
   Send, {BS}
Return
Perfect, should be marked as solved. Easy to add space block if needed.

Edit: There wasn't a request to solve something.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 104 guests