How to make a password edit text that only accepts the defined value and gives error when given different input.

3 views (last 30 days)
Hi I making a 4 digit password GUI in which when i put the correct password it opens up another GUI. The problem I am facing is that I am not able to compare the password when I am pressing the "Enter" pushbutton, I tried various "*is" functions but still couldn't get the desired output.
Here's my Code
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in key1.
function key1_Callback(hObject, eventdata, handles)
% hObject handle to key1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('1');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1,limit_text(4));
% --- Executes on button press in key2.
function key2_Callback(hObject, eventdata, handles)
% hObject handle to key2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('2');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key3.
function key3_Callback(hObject, eventdata, handles)
% hObject handle to key3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('3');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key4.
function key4_Callback(hObject, eventdata, handles)
% hObject handle to key4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('4');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key5.
function key5_Callback(hObject, eventdata, handles)
% hObject handle to key5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('5');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key6.
function key6_Callback(hObject, eventdata, handles)
% hObject handle to key6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('6');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key7.
function key7_Callback(hObject, eventdata, handles)
% hObject handle to key7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('7');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key8.
function key8_Callback(hObject, eventdata, handles)
% hObject handle to key8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('8');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in key9.
function key9_Callback(hObject, eventdata, handles)
% hObject handle to key9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('9');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in keyclear.
function keyclear_Callback(hObject, eventdata, handles)
% hObject handle to keyclear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit1,'string', '');
% --- Executes on button press in key0.
function key0_Callback(hObject, eventdata, handles)
% hObject handle to key0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
old=get(handles.edit1,'string');
new=('0');
new1=strcat(old,new); % add two different strings
set(handles.edit1,'string',new1);
% --- Executes on button press in keydel.
function keydel_Callback(hObject, eventdata, handles)
% hObject handle to keydel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
temp = get(handles.edit1,'String');
temp(end) =[]; %make the last/end entry blank [];
set(handles.edit1, 'String', [temp]);
% --- Executes on button press in keyenter.
function keyenter_Callback(hObject, eventdata, handles)
% hObject handle to keyenter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = get(handles.edit1,'String');
y = "6996";
if isempty(x)
set(handles.edit1,'String','');
elseif isreal(x) && length(x)==4 && isstring(y)
Mainhmi
else
set(handles.edit1,'String','');
end
Here's my Figure

Accepted Answer

Image Analyst
Image Analyst on 18 Sep 2022
When you want to check it, do this
truePassword = 'whatever';
userEntry = handles.edit1.String;
if strcmp(truePassword, userEntry)
% Correct password
else
% Incorrect password
warningMessage = sprintf('%s is not the correct password.\nTry again.', userEntry)
uiwait(warndlg(warningMessage))
% Clear bad entry from edit field.
handles.edit1.String = '';
end

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by