How to Hide Password and Show Entry Position in FoxPro

ID: Q113011

2.50 2.50a 2.50b 3.00 WINDOWS

The information in this article applies to:

SUMMARY

To prevent a password from being displayed during entry and to show the current editing position as well, a certain character can be echoed to the screen in place of the actual password characters, as demonstrated in the code below.

MORE INFORMATION

The following code, when placed in a program, will accept up to the number of characters specified in the variable PWLEN, allowing corrections with the BACKSPACE key. This value can be changed to allow fewer or greater characters in the password (minimum=1 character). If no characters are input or if all the characters have been previously deleted, the BACKSPACE key will be ignored when pressed. Attempting to enter more than the maximum number of characters will ring the bell to indicate that no more characters can be accepted; the additional characters will be ignored.

The variable W contains the character that will be echoed to the screen during entry. This value can be any ASCII character, although the asterisk (*), number sign (#), hyphen (-), or period (.) is most commonly used for this purpose.

NOTE: The FONT clause in this code example will be ignored in FoxPro 2.5x for MS-DOS, but must be removed if used with prior versions.

The ENTER key is required to terminate entry of the password, even if the maximum number of characters are used, in order to allow editing of the last character entered. This mimics the SET CONFIRM ON command option with FoxPro which prevents the user from typing through the end of a field accidentally.

Code Example

   *-------------- password code within a program
   CLEAR
   CLEAR ALL
   SET TALK OFF     && Prevents "double" echoing to the screen
   STORE '' TO x,y     && Initialize variables to a null value
   z=0               && Initialize INKEY variable to null character

   pwlen=10     && Maximum characters permitted in password
   w='*'          && Character used for display as placeholder

   * Display empty pseudo-GET field
   @2,2 SAY PADR(y,pwlen) FONT 'FoxFont',9 COLOR N/BG*

   DO WHILE z<>13
        z=INKEY(0,'H')     && 0=wait indefinitely, H=hide cursor
        * Check for exit by ENTER key press
        IF z=13
             EXIT
        ENDIF
        xlen=LEN(x)
        * Check for BACKSPACE while characters still left
        IF z=127
             IF xlen=0
                  LOOP
             ENDIF
             x=LEFT(x,xlen-1)
             y=LEFT(y,xlen-1)
        ELSE
             * If max, wait for ENTER (CONFIRM=ON effect)
             IF xlen=pwlen
                  ?? CHR(7)     && Ring the bell
                  LOOP
             ENDIF
             x=x+CHR(z)
             y=y+w
        ENDIF
        @2,2 SAY PADR(y,pwlen) FONT 'FoxFont',9 COLOR N/BG*
   ENDDO

Additional reference words: VFoxWin 3.00 FoxDos FoxWin 2.50 2.50a 2.50b mask encrypt encryption return KBCategory: KBSubcategory: FxprgGeneral
Keywords          : kbcode FxprgGeneral 
Version           : 2.50 2.50a 2.50b 3.00
Platform          : WINDOWS

Last Reviewed: May 22, 1998