2

I want to make progressive regex of PAN number.

My PAN number regular expression is "/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/"

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.

Community
  • 1
  • 1
Priyanka Pawar
  • 205
  • 1
  • 4
  • 16

4 Answers4

1

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!

http://plnkr.co/edit/x9x1ZT4InGHHo2f2ZL8f?p=preview

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
0

for pan validation regex in javascript is

let regExpe = /^([A-Z]){3}(C|P|H|F|A|T|B|L|J|G){1}([A-Z]){1}([0-9]){4}([A-Z]){1}?$/;

return regExpe.test(pan);

Explanation of regular expression

([A-Z]){3} :- first three characters can be in between A-Z only capital

(C|P|H|F|A|T|B|L|J|G){1} :- fourth character have special meaning

i.e. C stands for Corporate, P stands for Personal etc.

([A-Z]){1} :- fifth characters can be in between A-Z only capital

([0-9]){4} : next four character are numbers so it checks that these four characters should be digits

([A-Z]){1} :- last character also denotes only character in capital

after that finally I return the output in true and false by using javascript function

return regExpe.test(pan); (it returns true if pan is as per regex else it returns false)

0

^([A-Z]{3}[P]{1}[A-Z]{1}[0-9]{4}[A-Z]{1})?$

I can give you this piece of code:

<input
  data-ng-class="{'invalid-field-box': hasError( 'panCardInput' ) }"
  name="panCardInput"
  class="form-control ng-valid ng-scope ng-touched ng-dirty ng-valid-parse ng-not-empty"
  data-ng-model="registration.panNumber"
  ng-disabled="(alreadyLoggedIn &amp;&amp; ( !profileFromOtherSource || profileConfirmedFlag == 'Y' ) &amp;&amp; ( !profileCreatedByRecruiter || profileConfirmedFlag == 'Y' ) ) ||
RoleQuery.isOfferInitiated() || RoleQuery.isResumeShortlisted()"
  regex="^([A-Z]{3}[P]{1}[A-Z]{1}[0-9]{4}[A-Z]{1})?$"
  regex-msg="PancardCheck.error.regex"
  max-length="10"
  type="text"
  placeholder="Pan Card Number"
  input-validate=""
  style=""
/>

This code is from Tata's website, pretty sure they know what they are doing. And they frequently update their validations.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
-1

The regex you can use with matches() is formed based on the additional input from the users, and look-behinds check for the preceding 4th character. If the 4th letter is P, we check for the first letter in the surname, and if the 4th letter is not P, we check the first letter in the entity name:

String rx = "[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]";

Sample Code

String c1 = "S"; // First letter in surname coming from the EditText (with P before)
String c2 = "F"; // First letter in name coming from another EditText (not with P before)
String pan = "AWSPS1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCF1234Z"; // true
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));
pan = "AWSCS1234Z"; // false
System.out.println(pan.matches("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + c1 + "|(?<!P)" + c2 + ")[0-9]{4}[A-Z]"));

Click here

Community
  • 1
  • 1
  • Thank you for your answer but you did not understand my question. I don't want any extra validations on PAN regex. My regex works fine when I use ng-pattern etc. But to restrict input field I moved to directive and with this regex it does not allow me to enter input – Priyanka Pawar Jun 28 '16 at 11:32