Clear Filters
Clear Filters

How to restrict input string in length,symbols, char and numbers

4 views (last 30 days)
I am developing an app where the user inputs a string which need several restrictions. How can I check that the input is in the format 'LLL-NN-NNN' where L is lettes (A-Z) and N is numbers (0-9), and the string have to be exactly 10 characters in total.

Accepted Answer

Stephen23
Stephen23 on 7 Mar 2020
Edited: Stephen23 on 7 Mar 2020
>> str = 'XYZ-99-123'; % okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 1
>> str = 'XYZ-99-ABC'; % not okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
>> str = '666-99-123'; % not okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
>> str = 'hello world'; % not okay
>> ~isempty(regexp(str,'^[A-Z]{3}-\d{2}-\d{3}$','once'))
ans = 0
Use that logical output to either throw an error, or ask the user to supply new input data, or whatever suits your process.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!