Remove All Non-Alphanumeric Characters From an Answer

From HotDocs Wiki

Jump to: navigation, search

By default, users can enter almost any character in an answer for a Text variable. Sometimes, you may want to force users to enter only alphanumeric characters (letters and numbers only). The following computation examines the user's answer for a Text variable character by character, and if it finds anything other than a letter, number, or space, that character is removed from the answer.

The number of repetitions of the WHILE loop is based on the number of characters in the answer for the Text Var variable. Therefore, you must ensure that your template's Maximum WHILE iterations limit is large enough to process the answer. (You can specify this limit at the Component File Properties dialog box.)

CAUTION: This computation uses the WHILE and INCREMENT instructions, which work only in HotDocs 6.2 and higher.


SET Num Var TO 1 
WHILE Num Var <= LENGTH( Text Var ) 
  IF "abcdefghijklmnopqrstuvwxyz 0123456789" CONTAINS MID( Text Var, Num Var, 1 ) 
    INCREMENT  Num Var  
    SET  Text Var  TO  FIRST( Text Var, Num Var - 1 ) +  
    LAST( Text Var, LENGTH( Text Var ) - Num Var )  

Text Var Any Text variable. The computation will remove all non-alphanumeric characters from this variable's answer.
Num Var Any Text variable. A temporary Number variable used as a counter in the WHILE loop. (See Create a Temporary Variable.)