Validate user entry in field - Any improvements?

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
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Validate user entry in field - Any improvements?

Post by Simon Knight » Fri Jul 20, 2012 4:18 pm

Hi,

I am in the process of creating a stack where the user has to enter an IP address and port number similar to this example : 123.45.678.1:3000. I wanted to ensure that only valid characters were entered but I have discovered that this is not as easy as I initially thought. I offer my code for comment and improvement as I think I must have missed something obvious. I believe that my code is working but it is rather complex and long winded. I possibly should have used data entry fields rather than the one and ensured that the focus jumped from one to the next.

Code: Select all

-- Routine to process key presses and only allow the user to
-- enter a valid socket address.  Note the http:// is not subject to
--processing as it is static
-- http://                            123.1.213.12:3000

on KeyDown theKey
   
   put the length of  me into tlen
   put the last char of me into tLastChar
   put the text of me into tText
   
   -- first count the number of stop characters that exist in the string
   put ItemCount (tText,".") into tdotcount
   
   -- now determine which of the five sections of the IP address the user is attempting to add a character to
   put tdotcount+1 into tsectionNo
   
   --trap for first character being a stop
   if tlen =0 and thekey="." then
      exit keydown
   end if 
   
   -- now determine the number of characters in the last section
   -- by splitting the field text into an array and looking at the last section
   -- as determined by counting the stops / dots
   split tText by "."
   -- The last section is a special case - count the number of colons
   put ItemCount (tText[4],":") into tColonCount
   if tColonCount = 1 then 
      -- split the last section in two making five intotal
      put tText[4] into tLastSection
      split tLastSection by ":"
      --add the new sections to the array tText
      put tLastSection [1] into tText[4]
      put tLastSection [2] into tText[5]
      add one to tsectionNo
   end if
   put the length of tText[tsectionNo] into tCharCount
   -- trap for illeagle character pairs
   If thekey="." OR theKey=":" then
      If tLastChar = "." OR tLastChar = ":" then
         exit KeyDown
      end if
   end if
   -- At this point illegal pairs have been eliminated
   
   Switch tSectionNo
      Case 5 
         --deal with last section (5) which may have a colon
         
         Switch 
            Case tCharCount = 4
               --Next character must be a colon as long as previous was not a colon
               -- do nothing maximum length allowed
               break
            Case tCharCount < 4
               --Next character has to be a  number
               if thekey is a number  then 
                  put thekey after me
               end if
               break
         end switch
         break
      Case 4
         Switch
            Case tCharCount < 3
               --Next character may be a number or a colon
               if thekey is a number OR thekey is ":" then 
                  put thekey after me
               end if
               break
            Case tCharCount = 3
               if thekey is a number  then 
                  put ":" & thekey after me
               end if
               break
         end Switch
         break
      case 3
      case 2
      case 1
         -- sections 1-3
         -- count the number of existing characters in section
         Switch 
            Case tCharCount=3
               --Next character must be a stop unless previous char is a stop
               if thekey is a number then
                  put "." & thekey after me
               else
                  put "."  after me
               end if
               break
            Case tCharCount=0
               --Next character may be a number 
               if thekey is a number then 
                  put thekey after me
               end if
               break
            Case tCharCount<3
               --Next character may be a number or a stop
               if thekey is a number OR thekey = "." then 
                  put thekey after me
               end if
         end Switch
   end Switch
   
end KeyDown

function ItemCount pText, pDelimiter
   -- returns the number of times pDelimiter occurs in the text passed ptext
   put 0 into tItemcount
   repeat for each char tchar in ptext
      if tchar = pDelimiter then
         add one to tItemcount
      end if
   end repeat
   
   return tItemcount
   
end ItemCount
best wishes
Skids

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

Re: Validate user entry in field - Any improvements?

Post by dunbarx » Fri Jul 20, 2012 4:32 pm

Hi.

These are so much fun.

I am not sure I covered all you really intended, but try this and write back with what else is needed. I put the test address in a field 1.

Code: Select all

on mouseUp
   get fld 1
   set the itemdel to "."
   if the number of items of it = 4 and item 1 of it is an integer and item 2 of it is an integer\
   and item 3 of it is an integer and item 4 of it contains ":" then
      put item 4 of it into temp
      set the itemdel to ":"
      if item 1 of temp is an integer and item 2 of temp is an integer then
         answer "good to go"
      else
         answer "No go"
      end if
   else
      answer "No go"
   end if
end mouseUp
I hope you can add any other functionality to this scheme, such as testing for the length of each item. Is it true that the length of item 2, say, in "111.222.333" is limited to three digits? If so, you simply add : "...and the length of item 2 of it <=3..."

Craig Newman

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: Validate user entry in field - Any improvements?

Post by Simon Knight » Fri Jul 20, 2012 8:31 pm

Hi Craig,

Thanks for having a go. Am I correct in thinking that your code checks the completed field for validity? I am attempting to validate on each key stroke so that the user is not allowed to enter an incorrect set of numbers and full stops. The tricky bit is that as you mention each group may contain up to three numbers except the last which may contain more.

Simon
best wishes
Skids

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

Re: Validate user entry in field - Any improvements?

Post by dunbarx » Sat Jul 21, 2012 2:00 am

Ah. I get it. Still a fun problem in chunk engineering.

How about this in the field (named "tAddress") script:

Code: Select all

on keydown tKey
   put fld "tAddress" into temp
   set the itemDel to "."
   switch
      case (tKey is an integer or tKey = ".") and ":" is not in temp
         if the length of the last item of temp < 3 or the last char of temp = "." then put tkey after temp
         else put "." & tKey after temp
         
         if the number of items of temp = 4 then
            delete char the number of chars of temp - 1 to the number of chars of temp of temp 
            put ":" & tKey after temp
         end if
         break
      case tKey is an integer and ":" is in temp
         set the itemDel to ":"
         if the length of item 2 of temp < 4 then put tKey after temp
         break
   end switch
   put temp into fld "tAddress"
   select after fld "tAddress"
end keydown
Craig Newman

Post Reply