Formatting an input field

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

Post Reply
deebee
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 89
Joined: Mon Jul 19, 2010 6:59 am

Formatting an input field

Post by deebee » Wed Jan 26, 2011 6:04 am

I need to restrict the input of a field to a certain format but am having a difficult time thinking of how it should be done.

The input field needs to accept two formats:

99 XX 99999
or
99 X 99999

The first two digit number, the middle letter/s and the last five digit number each need stored in separate variables. The spaces should not be stored, and the shouldn't have to be typed, but they should automatically display is they are not typed.

Can this be done using only one input field? If so, could someone point me in the right direction?
Last edited by deebee on Wed Jan 26, 2011 12:53 pm, edited 1 time in total.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Formatting an input field

Post by jmburnod » Wed Jan 26, 2011 12:35 pm

Hi Debee,

If i understand what you want you need two differents inputs in the same fld.
How the fld know if it the first or the second input ?

Jean-Marc
https://alternatic.ch

deebee
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 89
Joined: Mon Jul 19, 2010 6:59 am

Re: Formatting an input field

Post by deebee » Wed Jan 26, 2011 12:50 pm

There is only one input at a time, it just needs to accept both formats.

I am using this to store product codes in a database and and just trying to reduce human error as much as possible. The product codes use only the two formats mentioned above, so that is what I would like to restrict the entry to.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Formatting an input field

Post by BvG » Wed Jan 26, 2011 2:21 pm

i think one of my live livecode code event presentations can help you, its exactly about formatting an input field:

http://blog.livecode.tv/page/10/

see also the attached stack "restricted entry.rev" linked on the same page.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

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

Re: Formatting an input field

Post by dunbarx » Wed Jan 26, 2011 6:03 pm

Are you typing into the field, or loading data from some other source? There are simple solutions once I know the answer.

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 121
Joined: Fri Mar 09, 2007 9:47 pm

Re: Formatting an input field

Post by jsburnett » Wed Jan 26, 2011 8:10 pm

I have a question about your problem.
I've been looking in to a way to solve it and been having fun.
Do you absolutely have to use only 1 field? or can you use 3 fields (and make it 'look' like 1 field?) ?
Thanks.
JSB

deebee
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 89
Joined: Mon Jul 19, 2010 6:59 am

Re: Formatting an input field

Post by deebee » Wed Jan 26, 2011 9:35 pm

Thanks BvG, I'll check it out later tonight hopefully.

The fields will be entered by keyboard.

Three fields that look like one would be fine. How do I do that?

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 121
Joined: Fri Mar 09, 2007 9:47 pm

Re: Formatting an input field

Post by jsburnett » Wed Jan 26, 2011 9:49 pm

If you can use 3 fields:

1)in the first field:

on keyDown keyPressed
if the selection is not empty
then put "" into me
if keyPressed is a number and the number of chars of me < 2 or keyPressed = tab
then pass keyDown
end keyDown

on keyUp
if the number of chars of me >= 2
then select the text of field 2
end keyUp

on tabKey
if the shiftKey is down
then
select the text of field 3
else
select the text of field 2
end if
end tabKey

2)in the second field:

on keyDown keyPressed
if the selection is not empty
then put "" into me
put chartonum(keyPressed) into charUsed
if charUsed >=65 and charUsed <=90 or charUsed >=97 and charUsed <=122 and the number of chars of me < 2
then pass keyDown
end keyDown

on keyUp
if the number of chars of me >= 2
then select the text of field 3
end keyUp

on tabKey
if the shiftKey is down
then
select the text of field 1
else
select the text of field 3
end if
end tabKey


3) in the third field:

on keyDown keyPressed
if the selection is not empty
then put "" into me
if keyPressed is a number and the number of chars of me < 5
then pass keyDown
end keyDown

on keyUp
if the number of chars of me >= 5
then select the text of field 1
end keyUp

on tabKey
if the shiftKey is down
then
select the text of field 2
else
select the text of field 1
end if
end tabKey

deebee
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 89
Joined: Mon Jul 19, 2010 6:59 am

Re: Formatting an input field

Post by deebee » Thu Jan 27, 2011 4:37 am

I think I'll use your three field method, JSB.
Thanks and glad you had fun with it!

jsburnett
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 121
Joined: Fri Mar 09, 2007 9:47 pm

Re: Formatting an input field

Post by jsburnett » Thu Jan 27, 2011 4:09 pm

Hi,

A more complete solution would include checking via numToChar conversion of the downKey - other keys to be passed but not 'typed' into the fields (aarow keys, return, enter, etc) - since these will be trapped.

I thought of this after submitting my response.

JSB

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

Re: Formatting an input field

Post by dunbarx » Thu Jan 27, 2011 9:57 pm

Here is a one field solution to the data entry poser. Put this into the field script:

Code: Select all

on keydown var
   
   switch the length of the currentData of me
      case 0
      case 1
         if var is not a number then
            set the currentData of me to the currentData of me & var
            put the currentData of me into me
         end if
         break
      case 2
         if var is a number then
            set the currentData of me to the currentData of me & space & var
            put the currentData of me into me
         end if
         break
      case 4
         if var is a number then  set the currentData of me to the currentData of me & var
         else  set the currentData of me to the currentData of me & space & var
         put the currentData of me into me
         break
      case 5
         if var is not a number then
            set the currentData of me to the currentData of me & space & var
            put the currentData of me into me
         end if
         break
      case 6
      case 7
      case 8
      case 9
         if var is not a number then
            set the currentData of me to the currentData of me & var
            put the currentData of me into me
         end if
         break
      case 10
          if var is not a number and the length of word 2 of the currentdata of me = 2 then
            set the currentData of me to the currentData of me & var
            put the currentData of me into me
         end if
         break
   end switch
end keydown

on deleteKey
   set the currentData of me to char 1 to -2 of the currentData of me
   delete the last char of me
   put the currentdata of me into me
end deleteKey

on backspaceKey
   set the currentData of me to char 1 to -2 of the currentData of me
   delete the last char of me
   put the currentdata of me into me
end backspaceKey
I see that this has deletion issues from the interior of the string, though it works fine from the end. Oh well...
You HAVE to love LiveCode!

Post Reply