Checking if inserted text has numbers

Hello there. I need to build a program, where an user inserts its name in the dialog box. But the inserted text cannot have numbers or any special symbols. How may program check that and show an information to user?

답변 (3개)

Niels
Niels 2017년 1월 6일

2 개 추천

in my opinion the easiest way would be
all(isletter(expressionString))
>> all(isletter('haj%kj'))
ans =
logical
0
>> all(isletter('hajkj'))
ans =
logical
1
Stephen23
Stephen23 2017년 1월 5일
편집: Stephen23 2017년 1월 5일

1 개 추천

>> all(isstrprop('onlyalpha','alpha'))
ans = 1
>> all(isstrprop('1234_+!%%','alpha'))
ans = 0

댓글 수: 4

Seems legit. But could you put it in the try-catch loop?
Stephen23
Stephen23 2017년 1월 8일
You can put this inside a try-catch statement, but as you don't mention this in your question I have no idea what you are trying to achieve.
It goes like this: you click on a button named "load patient", then a new window shows up, where you have to wirte its name. And I just want to prevent user from inserting symbols and numbers.
Stephen23
Stephen23 2017년 1월 9일
편집: Stephen23 2017년 1월 9일
I wouldn't use a try - catch for that. That is a bit brutal. Just put the code in a for-loop and break out of the loop when the user enters valid input. The for loop should have five or so iterations, after which you can conclude that the user is not interested in supplying valid data anyway so you might as well terminate the code.

댓글을 달려면 로그인하십시오.

José-Luis
José-Luis 2017년 1월 5일

0 개 추천

strTrue = 'Abs';
strFalse = '!abs!!!';
expr = '^[A-Za-z]';
a = isempty(regexp(strTrue,expr))
b = isempty(regexp(strFalse,expr))

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 1월 5일

편집:

2017년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by