Mask Password with asterisk in edit field used in app designer
Afficher commentaires plus anciens
Hello all, I have created a basic gui in app designer for user login.It has username selection as dropdown and password as edit field.
I want to mask the password with asterisk '*' while the user is typing the password with the edit field in focus. There are lot of resources on how to achieve this functionality in GUIDE but none in app designer.
Since edit field in app designer doesn't have 'keypress' Callback and also has no property 'String' (which are there in GUIDE) .I am finding it difficult how to replicate the same functionality in app designer.
I am using R2019a.Kindly help.
6 commentaires
Emine Ozkara
le 2 Juin 2020
Hello Sampath,
Could you figure out any solution?
Thanks.
Sampath Rachumallu
le 2 Juin 2020
Modifié(e) : Sampath Rachumallu
le 2 Juin 2020
zhi liu
le 11 Juin 2020
can you help me ,I can not achieve in R2020a in the way of yours and Sean
Sampath Rachumallu
le 16 Juin 2020
Bavitha Battipati
le 22 Avr 2021
Modifié(e) : Bavitha Battipati
le 22 Avr 2021
I'm facing the same issue. the password is not replaced by asterks. I'm using R2020b. Could you please help?
Sean de Wolski
le 23 Avr 2021
See my answer below.
Réponses (6)
Sean de Wolski
le 2 Juin 2020
Modifié(e) : Sean de Wolski
le 23 Avr 2021
8 votes
I would recommend using uihtml and the html password edit box.
Attached is a functional example (can be better formatted and whatnot)
EDIT: Robyn made this much easier with this FEX submission: Widgets Toolbox - MATLAB App Building Components - File Exchange - MATLAB Central (mathworks.com)
1 commentaire
Sampath Rachumallu
le 2 Juin 2020
I have created a sample app that just has one EditField (text)
- setup a public property, called PasswordVal
- add the UIFigureWindowKeyPress callback
- that function should follow below
- your password will be stored in PasswordVal, and can be used later anywhere in your app.
function UIFigureWindowKeyPress(app, event)
key = event.Key;
% Define letters, numbers, and special chars for your password. This is to
% stop the function from working when anything other than the defined chars
% above are entered.
letters = {'A','B','C','D','E','F','G','H','I','J','K',...
'L','M','N','O','P','Q','R','S','T','U','V',...
'W','X','Y','Z'};
Nums = {'1','2','3','4','5','6','7','8','9','0'};
SpecChars = {'!','@','#','$','%','^','&'};
% check to see if keypress is valid
if any(contains(letters,key,'IgnoreCase',true)) || any(contains(Nums,key,'IgnoreCase',true)) || any(contains(SpecChars,key,'IgnoreCase',true))
% key is valid, append to current password
app.PasswordVal = [app.PasswordVal,key];
% convert chars into *
app.EditField.Value = repmat('*',[1 length(app.PasswordVal)]);
else % invalid key, replce keypress with length of true password
app.EditField.Value = repmat('*',[1 length(app.PasswordVal)]);
end
end
Hope this helps.
1 commentaire
Sampath Rachumallu
le 2 Juin 2020
Modifié(e) : Sampath Rachumallu
le 26 Juin 2020
Matt Stead
le 23 Nov 2021
Modifié(e) : Matt Stead
le 26 Nov 2021
function password_entry(src, evt)
% password stored in src.UserData.password
% src == 'edit' textbox whose 'KeyPressFcn' == @password_entry
% src 'Callback' is disabled during entry, and called when enter/return hit
% there may be a better way to do this, but it works nicely
if (isempty(src.UserData)) % store password & callback in UserData
set(src, 'Interruptible', 'off'); % do not allow other key presses until this is done
src.UserData = struct("password", '', "callback", src.Callback);
src.Callback = [];
end
c = get(gcf, 'CurrentCharacter'); % char(evt.Key) will not get shifted characters
if (isempty(c)) % modifier key
return;
end
len = length(src.UserData.password);
if (c < 33 || c > 126) % non-printable characters
switch (c)
case {8, 127} % backspace, delete
len = len - 1;
src.UserData.password = src.UserData.password(1:len);
ast_str = repmat('*', [1 len]);
src.String = ast_str; % first time display
drawnow; % 'edit' builtin display replaces previous string
src.String = ast_str; % second display
return;
case {9, 10, 13, 27} % tab, newline, carriage return, escape
src.Callback = src.UserData.callback; % src callback will be called on return
set(src, 'Interruptible', 'on'); % reset default - not sure if this is necessary
return;
otherwise
return;
end
end
src.UserData.password(len + 1) = c;
ast_str = [repmat('*', [1 len]) c];
src.String = ast_str; % first time display (asterisks with last character)
drawnow; % 'edit' builtin display replaces previous string
src.String = ast_str; % second display
pause(0.2); % show asterisks with last character for a brief time
ast_str(end) = '*'; % replace with all asterisks
src.String = ast_str;
end
6 commentaires
Jignesh Solanki
le 20 Avr 2022
Matt,
I added UIFigureKeyPress callback and connected UIFigureKeyPress callback using existing callback from the Edit Field. Nothing is working from. Please can you explain in details how to use this password_entry function in App Designer 2021a.
Thanks.
Matt Stead
le 20 Avr 2022
Hi Jignesh,
I'm sorry, I don't use App Designer. Here's a textbox creation snippet from some of my code. It works, so I hope this is helpful to you. -matt
% Password Textbox
passwordTextbox = uicontrol(fig, ...
'Style', 'edit', ...
'Position', [(faxRight - TEXT_BOX_WIDTH) (faxTop - 93) TEXT_BOX_WIDTH TEXT_BOX_HEIGHT], ...
'BackgroundColor', 'white', ...
'FontSize', SYS_FONT_SIZE, ...
'FontName', 'FixedWidth', ...
'HorizontalAlignment', 'left', ...
'KeyPressFcn', @password_entry, ...
'Callback', @passwordTextboxCallback);
passwordTextbox.UserData.password = DEFAULT_PASSWORD;
passwordTextbox.String = repmat('*', [1 length(DEFAULT_PASSWORD)]);
Matt Stead
le 20 Avr 2022
Also, in case you were wondering, there's nothing special in the callback:
% Password Textbox
function passwordTextboxCallback(~, ~)
if (length(passwordTextbox.String) > MAX_PASSWORD_LENGTH)
errordlg('Password is too long', 'Error');
passwordTextbox.String = '';
end
end % passwordTextboxCallback()
Jignesh Solanki
le 20 Avr 2022
Matt: No clue what you are referrring in the above code. Please let me know how I can use above code in App Designer 2021a? I will need full details from which Componet Library need to be added and who is calling whom?
Thanks.
Matt Stead
le 20 Avr 2022
Hi Jignesh,
Here's a complete program that works on my machine.
Call as: pw = test_pw_box();
Hope this helps. You'll have to figure out how to use it with App Designer yourself though.
function [password] = test_pw_box()
DEFAULT_PASSWORD = 'test_password';
MAX_PASSWORD_LENGTH = 16;
SYS_FONT_SIZE = 12;
% Figure
fig = figure('Units','pixels', ...
'Position', [200 100 350 160], ...
'HandleVisibility','on', ...
'IntegerHandle','off', ...
'Renderer','painters', ...
'Toolbar','none', ...
'Menubar','none', ...
'NumberTitle','off', ...
'Name','Test Password Box', ...
'Resize', 'on', ...
'CloseRequestFcn', @figureCloseCallback);
% Axes
ax = axes('parent', fig, ...
'Units', 'pixels', ...
'Position', [10 10 330 140], ...
'Xlim', [1 330], 'Ylim', [1 140], ...
'Visible', 'off');
% Password Label
passwordLabel = text('Position', [100 100], ...
'String', 'Password:', ...
'Color', 'k', ...
'FontSize', SYS_FONT_SIZE, ...
'HorizontalAlignment', 'right', ...
'FontWeight', 'bold', ...
'FontName', 'FixedWidth');
% Password Textbox
passwordTextbox = uicontrol(fig, ...
'Style', 'edit', ...
'Position', [120 100 150 30], ...s
'BackgroundColor', 'white', ...
'FontSize', SYS_FONT_SIZE, ...
'FontName', 'FixedWidth', ...
'HorizontalAlignment', 'left', ...
'KeyPressFcn', @password_entry, ...
'Callback', @passwordTextboxCallback);
% Finished Pushbutton
finishedPushbutton = uicontrol(fig, ...
'Style', 'pushbutton', ...
'String', 'Finished', ...
'Position', [120 50 100 30], ...
'FontSize', SYS_FONT_SIZE, ...
'FontName', 'FixedWidth', ...
'HorizontalAlignment', 'left', ...
'Callback', @finishedPushbuttonCallback);
% Set focus to textbox
uicontrol(passwordTextbox);
% Password Textbox Callback
function passwordTextboxCallback(~, ~)
if (length(passwordTextbox.String) > MAX_PASSWORD_LENGTH)
errordlg('Password is too long', 'Error');
passwordTextbox.String = '';
passwordTextbox.UserData.password = '';
else
uicontrol(finishedPushbutton);
end
end % passwordTextboxCallback()
% Finished Pushbutton Callback
function finishedPushbuttonCallback(~, ~)
figureCloseCallback();
end % finishedPushbuttonCallback()
% Figure Close Callback
function figureCloseCallback(~, ~)
password = passwordTextbox.UserData.password;
delete(fig);
return;
end % figureCloseCallback()
uiwait;
end
function password_entry(src, ~)
% password stored in src.UserData.password
% src == 'edit' textbox whose 'KeyPressFcn' == @password_entry
% src 'Callback' is disabled during entry, and called when enter/return hit
% there may be a better way to do this, but it works nicely
if (isempty(src.UserData)) % store password & callback in UserData
set(src, 'Interruptible', 'off'); % do not allow other key presses until this is done
src.UserData = struct("password", '', "callback", src.Callback);
src.Callback = [];
end
c = get(gcf, 'CurrentCharacter'); % char(evt.Key) will not get shifted characters
if (isempty(c)) % modifier key
return;
end
len = length(src.UserData.password);
if (c < 33 || c > 126) % non-printable characters
switch (c)
case {8, 127} % backspace, delete
len = len - 1;
src.UserData.password = src.UserData.password(1:len);
ast_str = repmat('*', [1 len]);
src.String = ast_str; % first time display
drawnow; % 'edit' builtin display replaces previous string
src.String = ast_str; % second display
return;
case {9, 10, 13, 27} % tab, newline, carriage return, escape
src.Callback = src.UserData.callback; % src callback will be called on return
set(src, 'Interruptible', 'on'); % reset default - not sure if this is necessary
return;
otherwise
return;
end
end
src.UserData.password(len + 1) = c;
ast_str = [repmat('*', [1 len]) c];
src.String = ast_str; % first time display (asterisks with last character)
drawnow; % 'edit' builtin display replaces previous string
src.String = ast_str; % second display
pause(0.3); % show asterisks with last character for a brief time
ast_str(end) = '*'; % replace with all asterisks
src.String = ast_str;
end
Jignesh Solanki
le 23 Avr 2022
Thank you Matt.
Raph
le 20 Jan 2023
Matt Stead has a really good answer. For App designer, I had to do some modifications.
% add to your edit field the Value changing function
app.PasswordEditField.ValueChangingFcn= @app.password_entry;
% add a function in your appdesigner file.
function password_entry(app,src, evt)
% password stored in src.UserData.password
% src == 'edit' textbox whose 'KeyPressFcn' == @password_entry
% src 'Callback' is disabled during entry, and called when enter/return hit
% there may be a better way to do this, but it works nicely
if (isempty(src.UserData)) % store password & callback in UserData
set(src, 'Interruptible', 'off'); % do not allow other key presses until this is done
src.UserData = struct("password", '', "callback", src.ValueChangedFcn);
src.ValueChangedFcn = [];
end
c = get(app.UIFigure, 'CurrentCharacter'); % char(evt.Key) will not get shifted characters
if (isempty(c)) % modifier key
return;
end
len = length(src.UserData.password);
if (c < 33 || c > 126) % non-printable characters
switch (c)
case {8, 127} % backspace, delete
len = len - 1;
src.UserData.password = src.UserData.password(1:len);
ast_str = repmat('*', [1 len]);
src.Value= ast_str; % first time display
drawnow; % 'edit' builtin display replaces previous string
src.Value= ast_str; % second display
return;
case {9, 10, 13, 27} % tab, newline, carriage return, escape
src.ValueChangedFcn = src.UserData.callback; % src callback will be called on return
set(src, 'Interruptible', 'on'); % reset default - not sure if this is necessary
return;
otherwise
return;
end
end
src.UserData.password(len + 1) = c;
ast_str = [repmat('*', [1 len]) c];
src.Value = ast_str; % first time display (asterisks with last character)
drawnow; % 'edit' builtin display replaces previous string
src.Value = ast_str; % second display
pause(0.2); % show asterisks with last character for a brief time
ast_str(end) = '*'; % replace with all asterisks
src.Value = ast_str;
end
end
NIHAD
le 6 Avr 2024
0 votes
Hello, I would recommend to use "Editfield changing value function" Here is a functional sample code for app designer.
Joseph Reynolds
le 9 Oct 2025
Modifié(e) : Joseph Reynolds
le 9 Oct 2025
- Create a Edit Field (Text) for the password on the app in App Designer
- Right Click on the Field to Create a Call Back Function. Use PasswordEditFieldValueChanging Callback. This calls the function for each entery change.
- In the function Created Store the entries and update the Edit Field Value with an '*' :
function PasswordEditFieldValueChanging(app, event)
changingValue = event.Value;
delta = length(changingValue) - length(app.PasswordEditField.Value);
if delta > 0
app.password = [app.password changingValue(end)];
changingValue(end) = '*';
else
app.password = app.password(1:length(changingValue));
end
app.PasswordEditField.Value = changingValue;
end
end
Catégories
En savoir plus sur Develop Apps Programmatically dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!