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
}
Validating number in edit field
-
- Posts: 9
- Joined: 15 Mar 2016, 10:19
Re: Validating number in edit field
you can simply use the Number option when adding the Edit control:
https://autohotkey.com/docs/commands/Gu ... s.htm#Edit
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
-
- Posts: 9
- Joined: 15 Mar 2016, 10:19
Re: Validating number in edit field
Number option doesn't allow decimal points.
Re: Validating number in edit field
FYI - Your script doesn't work as posted. You need to remove the "v" from "vNumberentered" in your Check: subroutine, then it works again.
Re: Validating number in edit field
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:
I wrote this function:
Spoiler
If anyone can "break" it or knows a better/easier method, let me know.Re: Validating number in edit field
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
Re: Validating number in edit field
Perfect, should be marked as solved. Easy to add space block if needed.just me wrote:Code: Select all
Check: GuiControlGet, Var, , Numberentered If Var Is Not Number Send, {BS} Return
Re: Validating number in edit field
Perfect,just me wrote:Code: Select all
Check: GuiControlGet, Var, , Numberentered If Var Is Not Number Send, {BS} Return
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