Checking if inserted text has numbers

1 Ansicht (letzte 30 Tage)
Grzegorz Czyrski
Grzegorz Czyrski am 5 Jan. 2017
Bearbeitet: Stephen23 am 9 Jan. 2017
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?

Antworten (3)

Niels
Niels am 6 Jan. 2017
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 am 5 Jan. 2017
Bearbeitet: Stephen23 am 5 Jan. 2017
>> all(isstrprop('onlyalpha','alpha'))
ans = 1
>> all(isstrprop('1234_+!%%','alpha'))
ans = 0
  4 Kommentare
Grzegorz Czyrski
Grzegorz Czyrski am 9 Jan. 2017
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 am 9 Jan. 2017
Bearbeitet: Stephen23 am 9 Jan. 2017
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.

Melden Sie sich an, um zu kommentieren.


José-Luis
José-Luis am 5 Jan. 2017
strTrue = 'Abs';
strFalse = '!abs!!!';
expr = '^[A-Za-z]';
a = isempty(regexp(strTrue,expr))
b = isempty(regexp(strFalse,expr))

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by