Android Question How do I display a message when the user enters something that is NOT a number?

BTNorthrup

Member
Licensed User
Longtime User
I'm making an app where the user edits the text, but I don't want them to be able to use negative numbers or anything that is NOT a number. This is the piece of code I'm having an issue with, as I'm not sure what to replace "int" with.

If (TotalPay.Text < 0 OR TotalPay.Text <> int) Then
Msgbox("Please use only positive numbers and up to one decimal.","Error!")
End If

TotalPay.Text < 0 works perfectly fine, same as everything else in my app. I only need to replace "int" with something else.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
If TotalPay is an edittext, you could use TotalPay.INPUT_TYPE_DECIMAL_NUMBERS so that no letters can be used. But the minus sign is still available. You could delete it in the textchanged event. Then you would not need the if loop.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
As above or INPUT_TYPE_PHONE or something which means only digits are displayed..

Have you seen this? http://www.b4x.com/android/forum/th...l_numbers-on-galaxy-note-2.39160/#post-232533

I would keep the check in place, just in case...

You could do something like this and include the alphabet http://www.b4x.com/android/forum/threads/string-contain-function-and-an-array-of-items.44639/ You might want to consider upper and lowercase characters... or convert to upper or lowercase and then test for them being included..

A simple sub or similar which then returns true if the string contains alpha characters should suffice.
 
Upvote 0

BTNorthrup

Member
Licensed User
Longtime User
B4X:
If IsNumber(TotalPay.Text) = True AND INT(TotalPay.Text) < 0 THEN
     do something...
Else
    Msgbox("Here we are", Wrong value")
Thank you, everyone! Especially Beja. I started with what you said, tweaked it a bit to fit my code, and ended up with:
B4X:
    If IsNumber(TotalPay.Text) = False OR TotalPay.Text < 0 Then
     Msgbox("Please use only positive numbers and up to one decimal.","Error!")
Else (code...)

Works perfectly! :)
 
  • Like
Reactions: eps
Upvote 0
Top