Skip to content

Commit 785972f

Browse files
committed
PAN validation addition
1 parent 667086b commit 785972f

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Sanitizer | Description
194194
**trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input.
195195
**unescape(input)** | replace HTML encoded entities with `<`, `>`, `&`, `'`, `"`, `` ` ``, `\` and `/`.
196196
**whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`.
197-
197+
**isPAN(str)** | check if the string is a valid Indian PAN number.
198198
### XSS Sanitization
199199

200200
XSS sanitization was removed from the library in [2d5d6999](https://github.com/validatorjs/validator.js/commit/2d5d6999541add350fb396ef02dc42ca3215049e).

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ import isLicensePlate from './lib/isLicensePlate';
129129
import isStrongPassword from './lib/isStrongPassword';
130130

131131
import isVAT from './lib/isVAT';
132-
132+
export { default as isPAN } from './lib/isPAN';
133133
const version = '13.15.35';
134134

135135
const validator = {

src/lib/isPAN.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function isPAN(str) {
2+
const panRegex = /^[A-Z]{5}[0-9]{4}[A-Z]$/;
3+
return panRegex.test(str);
4+
}

test/validators/isPAN.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import isPAN from '../src/lib/isPAN';
2+
3+
describe('isPAN', () => {
4+
it('valid PAN', () => {
5+
if (!isPAN('ABCDE1234F')) throw new Error();
6+
});
7+
8+
it('invalid length', () => {
9+
if (isPAN('ABCDE123F')) throw new Error();
10+
});
11+
12+
it('lowercase should fail', () => {
13+
if (isPAN('abcde1234f')) throw new Error();
14+
});
15+
16+
it('wrong format', () => {
17+
if (isPAN('1234ABCDE1')) throw new Error();
18+
});
19+
20+
it('empty string', () => {
21+
if (isPAN('')) throw new Error();
22+
});
23+
});

0 commit comments

Comments
 (0)