Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Stephen23
Stephen23 il 7 Mar 2020
Modificato: Stephen23 il 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.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Tag

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by