How to check if text input is a number?

I need a number input from the user. How can I check whether this has been given? I’ve already set the keyboard to numeric, but users could still input non-number entries. In that case I would like to set a certain label text to: “Please enter a number.”

2 Likes

Hey @bramgrooten, thanks for posting to the community! :open_hands:

This sounds like a bug! Can you make a share link to your app for others to check out? This should not occur if you have the correct settings in place.

1 Like

Hey @bramgrooten

As a work-around you can multiply your input by 1 to “sanitise” the user data like this:

COuld this also serve as the logic?

if it’s even or odd, (or 0)
do the correct stuff with that value
else
let the user know they need to enter a number

2 Likes

never tried it - but definitely worth trying this one out too @bramgrooten

Thank you for the answers so far. I’m trying to build this app as an educational tool, where pupils will be coding as well. Thus, the code needs to be clean. The workarounds will unfortunately confuse the pupils. (Thanks for the help though!)

Hi @jared, thank you for your reply. I didn’t mean it as a bug. With a numeric keypad, users can still input a bunch of commas , for example, which is obviously not a number. I would like to be able to do a simple check whether or not the input is a number.

I would argue that @jbigg20189’s code is “clean” and that students need to be able to code a solution like that when one doesn’t exist (there isn’t already an isANumber [ ] block).

But another approach is to strip out any unwanted characters. On my iPhone, the only characters I can enter using a numeric keyboard are 0123456789. (zero through nine and a period). So something like this should work:

3 Likes

@tatiang’s method is probably the best way. You could strip unwanted but allowable characters, then you could put a pice of logic to not allow an empty text entry. It would correct for the mistakes, right?

I made some test and the problem is that users can actually “paste” a string in a PIN text box (even if the keyboard is set to numeric). I was thinking to create a variable with a list 0-9 and then test if the typed text is included in this list.
Also, in iOS is always present the “comma” in a numeric keyboard.
What do you think ?

Do you wan to make sure they dont enter numbers?

would it be easier to check if the pin the enter exists in a list. and if not, throw an error?

1 Like

Sure, but it shouldn’t matter what the user enters in the TextInput so long as you’re doing the data validation on the blocks side.

Jared has an example above that shows how to do this check:

1 Like

even or odd doesn’t work with numbers with comma

1 Like

can you try something like this? Take the input value, check for text, then remove the comma or anything else that shouldn’t be there.

2 Likes

@jared, i think the solution you provided is the best solution for this problem.
But, wait :hourglass: :hourglass:, just think yourself :, when user paste a text :computer: :computer:, instead of writing it, it can contain anything - like alphabets, numbers, special characters, non - ASCII character, ascii charcacter, smileys and unwanted chacters also, and you can’t just add them all in your, because then this list will go up to millions, billions, infinity, :scream: :scream: because characters are endless, in today’s world.
So, i think, the better solution will be checking right charcters and showing error if any character mismatched instead of checking wrong character matching.

1 Like