How come the number type in a textbox isn't working?

Get help using Construct 2

Post » Sat Apr 12, 2014 8:05 pm

I set the type of my textbox to number but I still can type letters. How to fix this??
B
37
S
14
G
3
Posts: 263
Reputation: 4,270

Post » Sat Apr 12, 2014 10:49 pm

The number type doesn't prevent you typing letters (as you know). It just tells the browser what type of input is expected and it's up to the browser how it handles that. For example, some mobile browsers would show a numeric input instead of the text keyboard.

If you want to enforce number input you need to check what's entered and remove anything that's not a number. Here's one way using regex (although this won't even allow a minus sign or decimal point):
onlyDigitsInTextbox.PNG
You do not have the required permissions to view the files attached to this post.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Post » Sat Apr 12, 2014 11:06 pm

@ramones Your solution worked but whenever I click any other button thats not numeric, it erases everything already entered in the box. Is there anyway to fix this?
B
37
S
14
G
3
Posts: 263
Reputation: 4,270

Post » Sun Apr 13, 2014 10:28 am

Hmm... I only tried in Firefox. In Chrome, it seems that TextBox.Text returns nothing when the input isn't a number. You could store the last value in an instance variable and if the text changes to "" then restore the old value. But then how do you tell the difference between the user typing a letter vs deleting all the text? It might be easier to leave the input type as 'Text'.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,630

Post » Sun Apr 13, 2014 2:08 pm

@ramones Yeah I guess. Also, is there anyway to get a permanent dollar sign to stay there? I making a bank for my game and I want an automatic dollar sign to show up when you enter your money in the textbox.
B
37
S
14
G
3
Posts: 263
Reputation: 4,270


Return to How do I....?

Who is online

Users browsing this forum: megatronx, RetrocadeMedia, totoe and 1 guest