Qbasicnews.com

Full Version: Passwords and Asterisks (*)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I thought that this would be a common topic, but after searching the posts, I didn't find anything, so... Big Grin

I would like to have my program set up so that when the user enters a password, their password will be replaced with an asterisk (*) for each character entered. I have tried using INKEY$ but to no avail.

Also, if the user enters the password wrong, he/she would key the backspace key to change the password. I would like the asterisks to display this, too. Again, I tried using INKEY$ with that, but for some reason I have never been able to get that to work.

Thanks for any help any one can give me.
You'd have to write your own input routine, if you're trying to use INPUT. For example:
Code:
Do
  do while len(xx$) = 0
    xx$ = inkey$
  loop
  pass$ = pass$ + xx$
  print "*";
Loop Until done = 1

print "Password entered was "; pass$

You'd have to code up a little thingy to check for backspace and unprintable characters ,but that shouldn't be too hard.
Code:
FUNCTION dinputPRINT$ (x1, question$, acceptable$, limit, col)

yy = CSRLIN 'yy = current y position

'set the color and coordinates, then print the question
COLOR col: LOCATE yy, x1: PRINT question$
  x$ = ""
  newx = LEN(question$) + x1 'place the new x coordinate after the question

DO 'loop until done

  i$ = INKEY$
  SELECT CASE i$
   CASE CHR$(8)         'backspace is pressed. What do we do?
    IF LEN(x$) > 0 THEN 'if the length of what has been typed is < 0...

     x$ = LEFT$(x$, LEN(x$) - 1) 'take off the last character
     xx = newx + 1               'reset the X coordinate
      
    'reprint the question
      LOCATE yy, x1: PRINT question$
    'print what has been typed
      LOCATE yy, xx: PRINT x$ + SPACE$(limit - LEN(x$))
    END IF
   CASE CHR$(13) 'enter has been pressed. Exit the loop
    EXIT DO
   CASE CHR$(27) 'escape has been pressed. dinputPRINT = escape code 99
    x$ = "99"
    EXIT DO
   CASE ELSE    'any other keys pressed?

    IF INSTR(acceptable$, i$) > 0 THEN 'if the key is an acceptable one...
     IF LEN(x$) < limit THEN          'if the string hasn't reached the limit
      x$ = x$ + i$                   'add what has been pressed to the string
      xx = newx + LEN(x$)       'set the x coodinate to the end of the string
    LOCATE yy, xx: PRINT  "*" 'i$ 'print the character
     END IF
    END IF
  END SELECT
  WAIT &H3DA, 8
LOOP
dinputPRINT$ = x$
END FUNCTION

LOCATE yy, xx: PRINT "*" 'i$ 'print the character
This is the line I changed to my dinput function to display * instead of the character. To specify the x & y, you'll have to use LOCATE and then the x value again in the function call :\
Thank you for your help. I will try this and see if it works for my needs :bounce:
Quote:I would like to have my program set up so that when the user enters a password, their password will be replaced with an asterisk (*) for each character entered. I have tried using INKEY$ but to no avail.

Also, if the user enters the password wrong, he/she would key the backspace key to change the password. I would like the asterisks to display this, too. Again, I tried using INKEY$ with that, but for some reason I have never been able to get that to work.

I recently wrote code to do exactly what you want...including backspace. An exe of the full program is at:
http://www.tcdn.teiher.gr/upload/downloa...fileid=682

Here's a working snippet of the password handling routine...this will run as a stand-alone program.

Code:
DECLARE SUB getpassword (word$, lin%)
SCREEN 0
COLOR 7
DEFINT A-Z

DO
CLS
PRINT "Enter Password for file"

pwa$ = ""
pwb$ = ""

lin = 0
CALL getpassword(pwa$, lin)
lin = 1
CALL getpassword(pwb$, lin)
IF pwa$ <> pwb$ THEN
PRINT
PRINT "Password not Confirmed"
PRINT
PRINT "try again"
t# = TIMER: DO UNTIL TIMER - t# > 1: LOOP    '2-second pause
CLS
END IF
LOOP UNTIL pwa$ = pwb$                  'valid password selected

SUB getpassword (word$, lin)
COLOR 7
IF lin = 0 THEN
  LOCATE 7, 1
  PRINT "        Password:";
ELSEIF lin = 1 THEN
  LOCATE 8, 1
  PRINT "Confirm Password:";
END IF
star$ = "*"
DO
a$ = INKEY$
IF a$ <> "" THEN
word$ = word$ + a$
SELECT CASE a$
CASE CHR$(8)                               '<backspace>
    IF LEN(word$) = 1 THEN
      word$ = ""
      GOTO outofhere                        'backspace + null-length
    END IF
    word$ = LEFT$(word$, LEN(word$) - 2)
    star$ = LEFT$(star$, LEN(star$) - 2)
CASE CHR$(13)                              '<enter>
    word$ = LEFT$(word$, LEN(word$) - 1)
    EXIT DO
END SELECT
COLOR 14
LOCATE 7 + lin, 19
PRINT SPACE$(LEN(star$) + 2)
LOCATE 7 + lin, 19
PRINT star$;

star$ = star$ + "*"
outofhere:
END IF
LOOP
COLOR 7
END SUB
Thank you Mango. The code portion works perfectly!

Nice program you wrote there, by the way. Very useful Smile
There are other posts out there that already ask this (although I cannot find them). Me and Zack set out once upon a time to build a full graphical login, but never finished it. However, I have the code for a working password prog, with encryption as well, which I will post as soon as I can find it.
Just put it into the FAQ...i think that would be the best place for it...

to all Newbies out there:

Don't forget to have a look into the FAQ before posting a question which is maybe already answered on the board or in the FAQ, ok?
Well people are showing there password programs, here's is mine:
Code:
CLS
PRINT "Enter Password: ";
row = 1
col = 17
LOCATE row, col
DO
press$ = INKEY$
IF press$ <> "" AND press$ <> CHR$(8) THEN
IF press$ = CHR$(13) THEN EXIT DO
LOCATE row, col
PRINT "*";
pswd$ = pswd$ + press$
col = col + 1
LOCATE row, col
END IF
IF press$ = CHR$(8) AND col - 1 >= 17 THEN
a$ = LEFT$(pswd$, LEN(pswd$) - 1)
col = col - 1
LOCATE row, col: PRINT " ";
pswd$ = a$
END IF
LOOP
It supports the backspace key, and the password entered is stored in the variable pswd$.
Code:
y% = CSRLIN
x% = POS(0)
p$ = ""
a$ = ""
  
DO
     LOCATE y%, x%
     PRINT "Enter password: "; a$; " ";
     k$ = INPUT$(1)
     SELECT CASE k$
          CASE CHR$(8)
               IF LEN(p$) THEN
                    p$ = LEFT$(p$, LEN(p$) - 1)
                    a$ = LEFT$(a$, LEN(a$) - 1)
               END IF
          CASE CHR$(13)
               EXIT DO
          CASE ELSE
               IF POS(0) < 80 THEN
                    p$ = p$ + k$
                    a$ = a$ + "*"
               END IF
     END SELECT
LOOP
Pages: 1 2