Page 1 of 1

Formatting an input field

Posted: Wed Jan 26, 2011 6:04 am
by deebee
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?

Re: Formatting an input field

Posted: Wed Jan 26, 2011 12:35 pm
by jmburnod
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

Re: Formatting an input field

Posted: Wed Jan 26, 2011 12:50 pm
by deebee
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.

Re: Formatting an input field

Posted: Wed Jan 26, 2011 2:21 pm
by BvG
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.

Re: Formatting an input field

Posted: Wed Jan 26, 2011 6:03 pm
by dunbarx
Are you typing into the field, or loading data from some other source? There are simple solutions once I know the answer.

Re: Formatting an input field

Posted: Wed Jan 26, 2011 8:10 pm
by jsburnett
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

Re: Formatting an input field

Posted: Wed Jan 26, 2011 9:35 pm
by deebee
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?

Re: Formatting an input field

Posted: Wed Jan 26, 2011 9:49 pm
by jsburnett
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

Re: Formatting an input field

Posted: Thu Jan 27, 2011 4:37 am
by deebee
I think I'll use your three field method, JSB.
Thanks and glad you had fun with it!

Re: Formatting an input field

Posted: Thu Jan 27, 2011 4:09 pm
by jsburnett
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

Re: Formatting an input field

Posted: Thu Jan 27, 2011 9:57 pm
by dunbarx
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!