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