How can I use :gsub for number-only text (both decimal, negative, positive)

How can I make it so my textbox can allow negative number
currently im using:

:gsub('[^%d.]', '')

Also I have been trying to find threads related to this, but I can’t understand. Sorry if this sounds dumb.

You would just include "%-". The percent sign is there because the dash has a special meaning in string patterns, so by putting a % in front of it you are cancelling out that meaning so that it is matched as an actual - sign.

"[^%d%.%-]"

By the way the period is a magic character as well so you have to escape it there too.

2 Likes

Thanks, I fixed it! (30 characters)