Check for numbers only

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Check for numbers only

Post by Nakia » Tue Feb 12, 2013 12:14 am

Hi,

I have a txt fld that I wish to validate as only containing numbers.
I am using the below and it works with the exception of when the fld contains more han 1 of the same number (example 22,11,55)

Suggestions on how to go about resolving..

Code: Select all

 if tTheFldText is in "0123456789" then

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Check for numbers only

Post by Simon » Tue Feb 12, 2013 12:43 am

Try:

Code: Select all

if isNumber(tTheFldText) then
isNumber is in the dictionary.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Check for numbers only

Post by dunbarx » Tue Feb 12, 2013 12:51 am

Hi.

Not sure about all that you said. For example, will your field contain items only (like you posted, "22,11,55")? Or some other format? Will it contain alphanumeric data like "A123"? And if so, do you want to strip the "A", or just tag the whole string?

Anyway, what you ask will be simple and fun to work out. As one of many possible approaches, you should look up the operator "is a/is not a". This might be used as follows, assuming the items in your example:

repeat for each item testItem in tFieldText
if testItem is not a number then put testItem & return after collectedBadData
end repeat

You will parse your data, either by character, item, word, or line, and test each of those chunks to see if they are a number. The engine has that power built in. You can collect the bad data or the good, or just delete the bad, or send an alert, or whatever you wish.

As a sidenote, your example would only work with individual characters tested one by one as per the above, and maybe that is a part of your script that you do not show. I suppose it would also work with the string "0123456789", but I bet that is not what you meant.

Anyway, play with this, and then try to do it a completely different way. Write back with what you discover.

Craig Newman

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Check for numbers only

Post by dunbarx » Tue Feb 12, 2013 1:00 am

Simon uses a more compact function "isNumber" instead of the operator "is a". That is perfectly valid.

But your example had "22,11,55" This is a string of three items, in LC parlance, all of which are numbers, it is true, but both Simon's and my offerings will fail if you try to test that whole string as a number. It is not. It is a string of three numbers separated by commas.

These must be dealt with separately.

Or is your data always one string of characters? If so, the problem becomes much simpler, and either the function or the operator can work in one go. Let us know how your data is formatted.

This does not mean you should not still play with the other stuff. You must.

Craig Newman

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Check for numbers only

Post by mwieder » Tue Feb 12, 2013 1:29 am

Or

Code: Select all

replace comma with empty in tTheFldText

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Check for numbers only

Post by Nakia » Tue Feb 12, 2013 1:52 am

isNuber worked the treat.

My example (22,11 etc) was just giving examples of different combo's of more than one number

Post Reply