How to Hide Password and Show Entry Position in FoxPro

Last reviewed: April 30, 1996
Article ID: Q113011
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, and 2.5b
  • Microsoft FoxPro for MS-DOS, versions 2.5, 2.5a, and 2.5b

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: kbprg kbcode
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.