Priyanka Pawar Priyanka Pawar - 1 year ago 89
AngularJS Question

How to make regular expression of PAN to progressive regular expression

I want to make progressive regex of PAN number.

My PAN number regular expression is


Format of PAN number -'AAAAADDDDA' A- Alphabets, D- Digits

Why I want to make progressive regex?

I am using angular directive to set validations on input field. In that I am facing problem to my PAN regex but it works fine for progressive regex. Check here my plunker

Referred question angularjs validate input and prevent change if invalid

Can any one please suggest me how to do this?

Thank you.

Answer Source

I think this should help you :

regexp = /^([a-zA-Z]([a-zA-Z]([a-zA-Z]([a-zA-Z]([a-zA-Z]([0-9]([0-9]([0-9]([0-9]([a-zA-Z])?)?)?)?)?)?)?)?)?)?$/;

Because you progressively want to test the regex, that's why it test the input letter by letter. ([a-zA-Z]){5} <- this meant it needs 5 letters (all at same time) because you were testing for every letter input that's why it did not work.

For example :

AAAAA1111A is correct according to your regex but AAAAA1111 or AAAAA111 or AAAAA11 or AAAAA1 etc are not, that's why your regex did not allow the input!