How do I check if a text variable is made only of numbers?

Just started using Construct 2? Post your questions here

Post » Thu Feb 23, 2017 11:42 pm

Hey, I've got a text variable that could contain letters, but I would like to check if it contains only numbers in order to trigger an event.
How can I check for this?

Thanks!
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646

Post » Fri Feb 24, 2017 3:15 am

A few ideas:
One would be to check if each character is a digit. You can extract indavidual characters with the mid() expression, and since you want to check each you'll want to use a loop. Use the Len() expression to get the number of characters. Next you either use a condition per digit or maybe use the find() expression to find the current character in some text of all the digits. Finally you could add 1 to a variable every time the character is a digit, then after the loop see if that variable is the same as the length of text.

Another way would be to convert the text to an int then back to text and compare it with the original text.
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Fri Feb 24, 2017 5:42 am

You might try regex.
Image ImageImage
B
171
S
50
G
179
Posts: 8,392
Reputation: 113,478

Post » Fri Feb 24, 2017 10:28 am

R0J0hound wrote:...Another way would be to convert the text to an int then back to text and compare it with the original text.

I think the second option is much easier =D
Do I just make a "number" variable and set it to my text or is there a better way of converting text into int?

Edit: Actually, that doesn't work, it outputs "NaN" to the int var...

newt wrote:You might try regex.

Thanks, but I think I'll go with R0J0's solution.

Edit: Oh sorry, I thought regex was a plugin =D
How do I use regex?
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646

Post » Fri Feb 24, 2017 11:29 am

Global text foo="123456"

System: compare variable foo=str(int(foo))
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Fri Feb 24, 2017 3:13 pm

R0J0hound wrote:Global text foo="123456"

System: compare variable foo=str(int(foo))

Thank you very much!
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest