3

Hi I don't know much about regular expression. But I need it in form validation using angularJs.

Below is the requirement

The input box should accept only if either

(1) first 2 letters alpha + 6 numeric

or

(2) 8 numeric

Below are some correct Inputs :-

(1)SH123456 (2)12345678 (3)sd456565

I tried data-ng-pattern="/(^([a-zA-Z]){2}([0-9]){6})|([0-9]*)?$/" , Its working fine for both the above condition but still it is accepting strings like S2D3E4F5 and may be many other combination as well.

What I am doing wrong I am not able to find it out.

Any help is appreciable !!!

Thanks

shreyansh
  • 1,637
  • 4
  • 26
  • 46
  • Possible duplicate of [In a regular expression, match one thing or another, or both](http://stackoverflow.com/questions/13351990/in-a-regular-expression-match-one-thing-or-another-or-both) – David R Sep 21 '16 at 09:51
  • @DavidR No, this is not a duplicate of that. It’s an anchoring problem. – tchrist Sep 22 '16 at 02:49

2 Answers2

7

In your regex, the two alternative branches are anchored separately:

  • (^([a-zA-Z]){2}([0-9]){6}) - 2 letters and 6 digits at the start of the string
  • | - or
  • ([0-9]*)?$ - optional zero or more digits at the end of the string

You need to adjust the boundaries of the group:

data-ng-pattern="/^([a-zA-Z]{2}[0-9]{6}|[0-9]{8})?$/"
                   ^                         ^^^^ 

See the regex demo.

Now, the pattern will match:

  • ^ - start of string
  • ( - start of the grouping:
    • [a-zA-Z]{2}[0-9]{6} - 2 letters and 6 digits
    • | - or
    • [0-9]{8} - 8 digits
  • )? - end of the grouping and ? quantifier makes it match 1 or 0 times (optional)
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    @revo: If someone downvotes your answers, probably there is something wrong with them. I did not express any concern, I only asked for a "constructive critique". Plain downvotes do not add value when the answer given is detailed, working and helpful. – Wiktor Stribiżew Sep 21 '16 at 10:33
  • In a normal case that's right but not times when **you** receive a down-vote. For sure I'll do what I said if I see the same behavior another time. – revo Sep 21 '16 at 10:37
  • @revo Please do it now. – Wiktor Stribiżew Sep 21 '16 at 10:38
  • I'll do it certainly but in a more appropriate time when I'm sure my complaint is going to take into account. – revo Sep 21 '16 at 10:43
3

You can try this DEMO LINK HERE

^(([a-zA-Z]{2}|[0-9]{2})[0-9]{6})?$

It will accept:

  1. ab123456
  2. 12345678
  3. aa441236
  4. aw222222
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Md Rahman
  • 1,812
  • 1
  • 14
  • 15
  • It is semantically the same as mine, so it will also work. The formal difference is just 1 nested capturing group is used here while mine has no nested groups. Both have similar performance. – Wiktor Stribiżew Sep 21 '16 at 10:03
  • 6 numeric values are common for both case. that's why I used a common part for 6 numeric values. and there are many ways to write regular expression. :) – Md Rahman Sep 21 '16 at 10:07
  • Equivalent to `^([a-zA-Z0-9]{2}[0-9]{6})?$`, it seems. – jensgram Sep 21 '16 at 10:08
  • No. your expression will also except a1234567. But its need First 2 letters alpha + 6 @jensgram – Md Rahman Sep 21 '16 at 10:11