简体   繁体   中英

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 "/^([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.

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

for pan validation regex in javascript is

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

return regExpe.test(pan);

Explanation of regular expression

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

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

ie C stands for Corporate, P stands for Personal etc.

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

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

([AZ]){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)

^([AZ]{3}[P]{1}[AZ]{1}[0-9]{4}[AZ]{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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM