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

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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!

  • 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.

  • You might try regex.

  • ...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...

    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?

  • Global text foo="123456"

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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Global text foo="123456"

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

    Thank you very much!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)