I know that Gui Edit fields have a parameter to only allow Numeric entry...but I was wondering if there was any way to only allow numbers OR letters. Or, heck, even just alpha entry. I ask this because I use the entry of these edit fields to define a particular variable name for easy identification in regards to other user choices, and having, say, an asterisk makes the variable name invalid. Any ideas?

Only allow Alphanumeric entry in an Edit field
Started by
iQATaylor
, Oct 02 2009 09:14 PM
6 replies to this topic
#1
-
Posted 02 October 2009 - 09:14 PM

Will this work for you?
It has a drawback that if an invaid character is typed, the cursor returns to the left of the edit box
It has a drawback that if an invaid character is typed, the cursor returns to the left of the edit box
prev=blank gui, font, s20, Verdana ; for my old eyes, change as needed gui, add, edit, w200 r1 vmytext galphanum , %prev% Gui, Add, Button, Default, OK gui, show return alphanum: gui,submit, nohide ; get the data from the control if mytext is alnum ; only allow alpha & numeric ; there are other choices, see http://www.autohotkey.com/docs/commands/IfIs.htm prev:=mytext else guicontrol,,mytext, %prev% return buttonOK: gui,submit, nohide msgbox !%mytext%! return esc:: exitapp
#2
-
Posted 02 October 2009 - 10:35 PM

Gui, Add, Edit, vEdit w200 Gui, Show OnMessage(0x102, "WM_CHAR") return WM_CHAR(wParam, lParam){ If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "i)^[a-z1-9\x08]$")) Return false } GuiClose: ExitApp
#3
-
Posted 02 October 2009 - 11:07 PM

Hey temp01,
That's Fantastic!
Can I subscribe to your newsletter? :wink:
That's Fantastic!

#4
-
Posted 02 October 2009 - 11:14 PM

Agreed, very nice temp01, I did not know you could do that.
#5
-
Posted 02 October 2009 - 11:59 PM


It has the same limitation as the "Number" option though:
Number: Prevents the user from typing anything other than digits into the field (however, it is still possible to paste non-digits into it).
so here's an updated version which detects EN_CHANGE message too so it works no matter how you input text:
Gui, Add, Edit, vEdit w400 Gui, Show OnMessage(0x102, "WM_CHAR") OnMessage(0x111, "WM_COMMAND") return WM_CHAR(wParam, lParam){ If(A_GuiControl = "Edit" and !RegExMatch(Chr(wParam), "i)^[a-z1-9\x16-\x1A\x03\x08]$")) Return false } WM_COMMAND(wParam, lParam){ GuiControlGet, hEdit, HWND, Edit If(hEdit=lParam) and (wparam>>16 & 0xffff=0x300){ ; EN_CHANGE GuiControlGet, text,, Edit If RegExMatch(text, RegEx:="i)[^a-z1-9]"){ SendMessage, EM_GETSEL:=0x00B0,, &End:=0xFFFFFFFF,, ahk_id %lParam% SendMessage, EM_SETSEL:=0x00B1, 0, End:=NumGet(End),, ahk_id %lParam% text := RegExReplace(SubStr(text, 1, End), RegEx) SendMessage, EM_REPLACESEL:=0x00C2, False, &text,, ahk_id %lParam% } } } GuiClose: ExitApp
#6
-
Posted 03 October 2009 - 12:21 AM

There MUST BE a newsletter :!: :wink: Please, pleaseHey temp01,
That's Fantastic!Can I subscribe to your newsletter? :wink:

#7
-
Posted 03 October 2009 - 12:31 AM
