Restrict input to 2 characters using numbers only

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Restrict input to 2 characters using numbers only

Post by morrell » Sun Dec 21, 2014 2:13 am

I want to have a field accept numbers only and no more than 2 characters.

I have tried this but the second part does not work:-

Code: Select all

on keyDown pKey 
if pKey is a number then 
pass keyDown 
else 
beep 
end if 
end keyDown

on keyDown pKey
if the length of me >= 2 then
beep
else
pass keyDown
end if
end keyDown
Regards,

Ruth

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

Re: Restrict input to 2 characters using numbers only

Post by Simon » Sun Dec 21, 2014 2:26 am

Hi Ruth,
You cannot use keyDown twice in one control.
See if you can mix them into 1, if you get stuck just ask.

tip: for what you asked for, ">=2" isn't what you want :)

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: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Restrict input to 2 characters using numbers only

Post by dunbarx » Sun Dec 21, 2014 3:29 am

Hi.

What Simon means is that if you have two handlers with the same name, only the first one will execute. This is actually really nice, because you can try several versions and store them in the same script, testing as needed. Also, if you have a handler that works, but want to try a variation, you copy it below itself, and work on the top one. Then if you screw that one up, you have the good one there for reference.

As to combining them, again as Simon mentioned, you are so close already...

Craig Newman

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Restrict input to 2 characters using numbers only

Post by Dixie » Sun Dec 21, 2014 11:07 am

In the script of a field..

Code: Select all

on keyDown theKey 
   if theKey is not a number then 
      beep
      exit keyDown
   end if
   
   if the number of chars of me < 2 then
      pass keyDown
   else
      beep
   end if
end keyDown

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Restrict input to 2 characters using numbers only

Post by morrell » Sun Dec 21, 2014 2:29 pm

Thank you Dixie.

Without success I was working on the problem but not using if and I actually did solve my problem with the code below, but along the way I read somewhere on here that switch should not be used with keydown.

Regards,

Ruth

Code: Select all

on keyDown theKey
switch
case theKey is a number and length(me) <2
pass keyDown
end switch
beep
end keyDown]

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

Re: Restrict input to 2 characters using numbers only

Post by Simon » Sun Dec 21, 2014 2:55 pm

Excellent Ruth!
Not sure where you got "length(me)" it can be "length of me".
Not sure about the switch and keydown not being used together.

With Dixe's reply and Ruth only thanking him, I wonder if people want this forum to be more like stackoverflow? Working out the answers is not what people want to do?

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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Restrict input to 2 characters using numbers only

Post by Dixie » Sun Dec 21, 2014 3:14 pm

Simon..

With the greatest respect... I think that people post the problems that they are having with liveCode here in the forum because they have hit a 'brick wall'... they want the solution to their problem so that they can move on with their code... I for one always smile wryly when I see the word 'homework' suggested in the reply...

Many people though seem to be reluctant to post their scripts to the point where errors are thrown.. there is often not enough background given regarding what they wish to achieve to help give a 'sensible' or 'working' reply...

Dixie

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

Re: Restrict input to 2 characters using numbers only

Post by dunbarx » Sun Dec 21, 2014 6:24 pm

I am the homework guy.

This is all a matter of style. I like to push users to work things out for themselves if at all possible, trying to gauge their skill level, and only within their means. The stackOverflow method hands the solution over on a platter. In fact it is required there. Fine and good; there is no right way to help people. But I think that for new users, hints and homework, carefully delivered within the abilities of that user, are more useful and far more helpful. I would never take such a tack with, say, Mark Waddingham.

Of course he has never asked me for help.

Simon is fairly in my camp. But none of this matters. What I want is for users to learn about, and love, this environment. So does he, so does everyone who takes the time to do this work.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Restrict input to 2 characters using numbers only

Post by jacque » Sun Dec 21, 2014 7:15 pm

I'm with Dixie. I learned by studying scripts. When you are first starting there is so much to learn and the frustration level can be very high. Telling someone who has posted here in frustration to do homework can be the final straw that causes them to throw in the towel. I don't want to make things worse.

As they learn, pointing them to dictionary entries is more appropriate. But I want to preserve the love of LiveCode, and making a blocked and sometimes angry user paddle around in the swamp won't do that.

So yeah, I don't think of this forum as a classroom and unless the poster asks specifically for only a hint, I have no problem giving them the answer. They will learn from it. The one exception is a student who is trying to complete a real homework assignment. If it sounds like that then hints are the only thing I'll offer.

Craig, we both learned back on the HC list. I remember only generous people posting scripts as examples, sometimes multiple versions. It was fun, not work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Restrict input to 2 characters using numbers only

Post by phaworth » Sun Dec 21, 2014 8:03 pm

This is a classic case of why duplicate handler names should not be allowed. I can't think of anywhere else in Livecode that duplicate names are allowed and as the OP discovered they can cost frustrating debugging time. The situation is made worse because duplicate handler names are only listed once in the Script Editor's list of handler names so there's no clue they exist unless you did it on purpose.

I know that some folks make use of this "feature" but there other ways to achieve what they want. Just comment out the versions of the handler you don't want to execute or use on of the plugins that allows you to crate a copy of your stack and rollback to it

There's been discussion on this in the past and in deference to those who make use of this feature, I think allowing duplicate handler names should be optional or at the very minimum make their presence visible in the list of handlers. Strict Compilation Mode would be my choice - it's it's on, duplicate handlers cause a compile error, if not go ahed and allow duplicates but indicate they exist in the script editor list of handlers.

I filed QCC report #12849 on this a few months ago.

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Restrict input to 2 characters using numbers only

Post by dave.kilroy » Sun Dec 21, 2014 8:18 pm

If someone has been trying to figure out a problem and comes to the forum after expending effort - then (depending on my level of laziness at the time) I'm happy to provide code to help out (if it's needed, often it isn't) - helping out is a pleasure. But in this situation I would also include something to explain the code or the problem (unless I thought the skill level of the person was such that he/she could figure it out from the code).

What I do very much appreciate are all the postings which people provide - between us all we make the difference - Dixie's code above without Simon's and Craig's posting wouldn't have had the same effect on the OP - often it is the background discussion and questions back to the user that can be more helpful than just code

And finally (I'll stop in a minute) I do think that when we ourselves are recipients of help we should thank EVERYONE who has taken part in group-helping-process
"...this is not the code you are looking for..."

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

Re: Restrict input to 2 characters using numbers only

Post by dunbarx » Mon Dec 22, 2014 12:07 am

Jacque.

I usually post code that gives a push in the right direction, or solves a fundamental aspect of the question, and only then ask that the user go that extra mile. Only rarely will I post a full solution, though I have done so, but mainly to competent programmers, in others words, already at an intermediate level, where I am collaborating, rather than teaching.

Certainly to a serious new user, who will examine the proffered code instead of just inserting it and moving on, the opportunity to study well formed scripts is in its own right a good teaching tool. Perhaps I suspect the former happens too often, and an opportunity is lost. I exhort more than most that the user do just that, but then try to do it another way, and then try to do it backwards, whatever.

I think the forum environment itself, as Dave pointed out, is synergetic. To a serious newbie, the several styles of support are likely to be appreciated as given. At any rate, none of this matters much; the passion of the helper community is the only thing that does.

I am teaching LC to a troubled teen who wants to learn how to program. We are making toys, buttons that "breathe" in width, bounce between two walls, that sort of thing. I suggest a task, he suggests the logic (usually spot-on), I point out the tools needed, he screws it up royally, I give a running start, he gets close, ...you get the picture. I cannot see doing this any other way.

Given the remoteness of the forum, the distance between the helpers and the user, may suggest that working code is more suitable. Well, maybe, partially.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Restrict input to 2 characters using numbers only

Post by jacque » Mon Dec 22, 2014 12:28 am

dunbarx wrote:I am teaching LC to a troubled teen who wants to learn how to program. We are making toys, buttons that "breathe" in width, bounce between two walls, that sort of thing. I suggest a task, he suggests the logic (usually spot-on), I point out the tools needed, he screws it up royally, I give a running start, he gets close, ...you get the picture. I cannot see doing this any other way.
This is truly honorable. Kudos. I think in the end we'll each just continue doing what we do, and everyone will benefit.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

morrell
Posts: 84
Joined: Sat Dec 13, 2014 1:12 am

Re: Restrict input to 2 characters using numbers only

Post by morrell » Mon Dec 22, 2014 10:10 am

I'm probably the newest person on here and my first experience of requiring help with livecode is here http://forums.livecode.com/viewtopic.php?f=7&t=22331 and it took me 9 days to finally complete that problem (includes days before the Post). Probably some of the delay is my failing to fully explain my problem which from now on I will improve.

Obviously I'm not the norm as there can't be many 71 years of age grandmothers attempting to write an App with no experience of any real computer coding, other than html and I'm not suggesting I would pass any exam on that subject. It was only because one of my late husbands friends asked me if I could send him a particular Excel file that he knew my husband had that I thought, that would make a good App and so I was lucky to come upon livecode in my 'how will I do it'. I have to admit there was a low point when I was so frustrated at getting nowhere, that it was not until my grandsons who are in their late 20s said "Nana, you will never succeed as it will take years to learn all that" (time is not exactly on my side) and that put the bit between my teeth.

So what do I prefer regarding answers to questions? Well I prefer you kind people to give the correct code, as I have already spent many hours trying to work it out myself as I hate asking for help, then if possible below the answer give tips on where I should have researched to arrive at your answer. So keep up the good work no matter what your preferred answer method is, as it is greatly appreciated,

Kind regards,

Ruth

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

Re: Restrict input to 2 characters using numbers only

Post by Simon » Mon Dec 22, 2014 10:41 am

Ohhhhh!
You are "That Ruth"
Sorry for not recognizing you, yes some of us do read many posts.
Had I made the connection I would have answered differently.
I am all for you succeeding in your endeavor.

Do I have favoritism? Yep! Have many people said "Do the work for me". Yep (errr, not in those words).

Your "switch/case" answer says that in no time you'll know more than me.

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

Post Reply