How to accept only positive numbers on textbox ?
Show older comments
Hello;
I need to put only positive number in textbox. It's a equation and about time so it's cant be negative. Which codes help me ?
S = str2num(char(get(handles.edit1 , 'String')));
if (isempty(S))
set(hObject,'String','0');
warndlg('Insert positive value','!! Warning !!');
end
I tried someting, If I put letters in textbox that give me a warning (working) but I need to change that with positive numbers.
Accepted Answer
More Answers (1)
Matas Mendelis
on 18 Dec 2020
It's best to use a while loop after every imput. This is my code to force the user only imput possitive spring stiffness values. I also added a warning message box.
promt='front spring stiffness(N/mm)';
F_Kspring=input(promt);
if F_Kspring<=0 %warning message box
f = warndlg('Insert only POSITIVE values');
end
while F_Kspring<=0 %While loop for making user insert only possitive values
promt='front spring stiffness(N/mm)';
F_Kspring=input(promt);
end
2 Comments
Rik
on 18 Dec 2020
I would avoid having the same code twice:
promt='front spring stiffness(N/mm)';
while true %While loop for making user insert only possitive values
F_Kspring=input(promt);
if F_Kspring<=0 %warning message box
f = warndlg('Insert only POSITIVE values');
else
break
end
end
Matas Mendelis
on 20 Dec 2020
Yes, this is better, then my original. Neater and the warning box appears every time when negative value is inserted.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!