Articles Archive
Articles Search
Director Wiki

Taking out extra spaces

December 25, 1997
by Pat McClellan

Dear Multimedia Handyman

I want to replace any blank spaces in an editable text field with underscores when I put the inputed info into a variable. What's the best way to do this?

Ham Lee


Let's assume that you have an editable field, into which your user types some text and then presses the Return key to enter it. Attach this behavior to the user input field.

on keyUp me
  if the key = RETURN then
  end if

Now we need to create a handler called "processInput" which will look through the user input field, letter by letter. The handler uses an if statement to test each letter to see if it is a space, replacing any spaces with underscores. When all letters have been checked, the resulting string can be processed as needed. In this example, I am putting the resulting string into another field so you can see the result. The original input field remains unchanged.

on processInput
  set tempString = the text of field "userInput"
  set totalLetters = the number of chars in tempString
  repeat with i = 1 to totalLetters
    if char i of tempString = " " then 
     -- that's a space
      put "_" into char i of tempString
    end if
  end repeat
  put tempString into field "processedText"

Now, let's do something a little more complex with user input. Let's say that you want the user to enter her/his social security number. For our non-US readers, the format for this number is XXX-XX-XXXX, where all characters are numerical. So, for this input, we want to limit the possible characters to number. It would also be nice if the dashes filled themselves in automatically.

In this case, we need to process each character as it is entered. If it is a number, we will accept the input; but if the character is a letter, we will ignore it. To handle the dashes, we'll count the number of characters that have been accepted. We'll insert dashes after characters 3 and 6 (the first dash will count as char 4). Finally, when 11 characters are displayed (9 numbers plus 2 dashes), we'll make the field un-editable.

This is the behavior script attached to the field. Note the use of a case statement instead of multiple if statements. If the key is not a number, the keyDown event is not passed, so the character doesn't appear in the field. Try entering non-numerical characters and see the result.

on keyDown me
  case the key of
    "0": put the key after field "ssn"
    "1": put the key after field "ssn"
    "2": put the key after field "ssn"
    "3": put the key after field "ssn"
    "4": put the key after field "ssn"
    "5": put the key after field "ssn"
    "6": put the key after field "ssn"
    "7": put the key after field "ssn"
    "8": put the key after field "ssn"
    "9": put the key after field "ssn"
  end case
  set charCount = the number of chars in field "ssn"
  if charCount = 3 or charCount = 6 then
    put "-" after field "ssn"
  end if
  if charCount = 11 then -- number is complete
    set the editable of member "ssn" to false
  end if

NOTE: the sprite for the entry field is NOT set to editable. Instead, I set the editable of member "ssn" to TRUE in my startMovie script and on the RESET button script. This allows me to turn OFF the editable of member "ssn" in the handler -- regardless of what its sprite channel might be.

This same concept can be used in a variety of ways, such as to exclude numerical character or punctuation from name fields. We could have used a similar approach with the spaces to underscore task, but in that case, I didn't see the need to run the process after each character entry. It seemed more efficient to just run it once at the end.

Experiment with the many ways you can limit and process user entry. I recommend reading the sections of the Lingo Dictionary regarding key, keyCode, keyPressed, keyDownScript, and keyUpScript. Also, review Chapter 7 of "Learning Lingo". It's a great way to see what tools you have -- at the user's fingertips.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2019, Director Online. Article content copyright by respective authors.