I need to validate an input field that can have the following formats:
'D-1' ou 'D-10' ou 'D-1_1' ou 'D-10_1' ou 'D-1A' ou 'D-10A'
Letters and numbers may vary, but will always have one of these formats.
I need to validate an input field that can have the following formats:
'D-1' ou 'D-10' ou 'D-1_1' ou 'D-10_1' ou 'D-1A' ou 'D-10A'
Letters and numbers may vary, but will always have one of these formats.
Use this regex:
([a-zA-Z]+-\d+_{0,1}[a-zA-Z0-9]*)
Explanation:
[a-zA-Z]+
Valid if one or more than one letter exists. -
Validates whether the -
character exists after the letter. \d+
Valid if one or more number exists. _{0,1}
Checks if after the previous sequence there exists the character _
if it exists it enters the capture group. [a-zA-Z0-9]*
Check if there are any letters or numbers after _
, if these characters are entered in the capture group.