neha arora
neha arora

Reputation: 31

Validation:Allow Numeric and Alphanumeric Pattern

a textBox can contain values in format

DL-06-T-7405 (first two Alphabets then two Numbers then Alphabet and next four are Numbers) or DL-06-TT-7405(first two Alphabets then two Numbers then two Alphabet and next four are Numbers) or DL-06-TTT-7405(first two Alphabets then two Numbers then three Alphabet and next four are Numbers)

what i have done so far..

/^[A-Z]{2}[0-9]{2}\w$/

but i can not make other cases case 1: DL-06-TTT-7405 case 2: DL-06-TT-7405 case 3: DL-06-T-7405

how can i allow a textbox takeabove 3 cases only not other than this

Upvotes: 2

Views: 90

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627536

You may use a limiting quantifier {1,3} to match 1 to 3 occurrences, and add hyphens in between subpatterns:

^[A-Z]{2}-[0-9]{2}-[A-Z]{1,3}-[0-9]{4}$
                        ^^^^ 

See the regex demo

Pattern details:

  • ^ - start of string
  • [A-Z]{2} - 2 uppercase ASCII letters
  • - - a literal hyphen
  • [0-9]{2} - 2 digits
  • - - - hyphen
  • [A-Z]{1,3} - 1 to 3 uppercase ASCII letters
  • -[0-9]{4} - a hyphen, 4 digits
  • $ - end of string.

Upvotes: 1

Related Questions