|
Send Message behavior
This script is attached to the Button member.
-- Send Message behavior
-- This behavior sends a message to check the number when
-- the behavior's button has been clicked. on mouseDown me
sendAllSprites(#checkNumber)
end
Check Number behavior
This script is attached to the Display Field member.
-- Check Number behavior
-- This behavior verifies that a text in a field is a
-- valid zip code: a number containing five digits.
-- You attach this behavior to a field sprite.
-- This handler checks the text and displays an
-- alert dialog box when it finds an error.
on checkNumber me
-- Stores in the testString variable the text
-- the user entered in the behavior's field.
set currentSprite to the spriteNum of me
set currentMember to the member of sprite currentSprite
set testString to the text of member currentMember
-- Verifies that the string is a valid zip code number
-- and displays an alert that describes the error when the
-- string is not valid.
set errorFound to #none
if testString contains " " then
set errorFound to #spaces
else
if length(testString) > 5 then
set errorFound to #tooLong
else
if length(testString) < 5 then
set errorFound to #tooShort
else
if voidP(value(testString)) then
set errorFound to #notNumber
end if
end if
end if
end if
displayError(me, errorFound)
end
-- This handler displays an alert, based on the symbol in
-- the errorFound parameter.
on displayError me, errorFound
case errorFound of
#none: alert "This is a valid zip code."
#spaces: alert "A zip code can not contain spaces."
#tooLong: alert "A zip code can't have more than 5 digits."
#tooShort: alert "A zip code can't have less than 5 digits."
#notNumber: alert "A zip code contains only numbers"
end case
end
|