How to make textbox only allow alpha-numeric characters and underscore

How would I make so TextBox will only limit it’s text to (A-Z), (0-9), and _ underscore?
I can’t find any solution online and I’m new to Lua regexes.

My sample code:

	TextBox.Text = TextBox.Text:gsub('[^%w]', '')

Basically it does is when you type anything from the textbox, it removes any non alpha-numeric characters. However, the underscore symbol is not part of the alpha-numeric characters so it will filter it off.

Any help would be appreciated!

The pattern is correct, you just have to add underscore on the regex
Text:gsub('[^%w_]', '')

This make sure that only non alpha-numerical characters and underscore wont get deleted.
Hope this helps!