Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Only allow Alphanumeric entry in an Edit field


  • Please log in to reply
6 replies to this topic
iQATaylor
  • Members
  • 11 posts
  • Last active: Oct 05 2009 11:15 PM
  • Joined: 27 Oct 2008
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?

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
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
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


temp01
  • Members
  • 120 posts
  • Last active: May 18 2013 08:27 PM
  • Joined: 09 Jul 2009
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


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Hey temp01,
That's Fantastic! :D Can I subscribe to your newsletter? :wink:

entropic
  • Members
  • 181 posts
  • Last active: Nov 27 2011 03:15 AM
  • Joined: 21 Dec 2008
Agreed, very nice temp01, I did not know you could do that.

temp01
  • Members
  • 120 posts
  • Last active: May 18 2013 08:27 PM
  • Joined: 09 Jul 2009
:)
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


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

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

There MUST BE a newsletter :!: :wink: Please, please :(