Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for IL in isIdentityCard() #1041

Merged
merged 7 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Validator | Description
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
Expand Down
27 changes: 27 additions & 0 deletions lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ var validators = {
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': function heIL(str) {
var DNI = /^\d+$/; // sanitize user input

var sanitized = str.trim(); // validate the data structure

if (!DNI.test(sanitized)) {
return false;
}

var id = sanitized;

if (id.length !== 9 || isNaN(id)) {
// Make sure ID is formatted properly
return false;
}

var sum = 0,
incNum;

for (var i = 0; i < id.length; i++) {
incNum = Number(id[i]) * (i % 2 + 1); // Multiply number by 1 or 2

sum += incNum > 9 ? incNum - 9 : incNum; // Sum the digits up and add to total
}

return sum % 10 === 0;
},
'zh-TW': function zhTW(str) {
var ALPHABET_CODES = {
A: 10,
Expand Down
25 changes: 25 additions & 0 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ const validators = {

return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': (str) => {
const DNI = /^\d+$/;

// sanitize user input
const sanitized = str.trim();

// validate the data structure
if (!DNI.test(sanitized)) {
return false;
}

const id = sanitized;

if (id.length !== 9 || isNaN(id)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@profnandaa isn't this check unnecessary if we add the size limit in the regex?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's true. That was oversight from my end. You can do a quick update PR on this?

const DNI = /^\d{9}$/;

cc. @perimiter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats true, but you still need the if statement for the isNaN()

Copy link
Member

@tux-tn tux-tn Jun 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@perimiter your regex is searching only for digits, the function will always exit before the call to isNaN if it's not a number.
As you can see, the path is never taken in the tests
Capture d’écran

Copy link
Contributor Author

@perimiter perimiter Jun 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tux-tn your right, i opened a PR with the new changes (#1048)

// Make sure ID is formatted properly
return false;
}
let sum = 0,
incNum;
for (let i = 0; i < id.length; i++) {
incNum = Number(id[i]) * ((i % 2) + 1); // Multiply number by 1 or 2
sum += incNum > 9 ? incNum - 9 : incNum; // Sum the digits up and add to total
}
return sum % 10 === 0;
},
'zh-TW': (str) => {
const ALPHABET_CODES = {
A: 10,
Expand Down
39 changes: 39 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3062,6 +3062,41 @@ describe('Validators', () => {
'Z1234567C',
],
},
{
locale: 'he-IL',
valid: [
'219472156',
'219486610',
'219488962',
'219566726',
'219640216',
'219645041',
'334795465',
'335211686',
'335240479',
'335472171',
'336999842',
'337090443',
],
invalid: [
'123456789',
'12345678A',
'12345 678Z',
'12345678-Z',
'1234*6789',
'1234*678Z',
'12345678!',
'1234567L',
'A1234567L',
'X1234567A',
'Y1234567B',
'Z1234567C',
'219772156',
'219487710',
'334705465',
'336000842',
],
},
{
locale: 'zh-TW',
valid: [
Expand All @@ -3088,6 +3123,10 @@ describe('Validators', () => {
'X1234567A',
'Y1234567B',
'Z1234567C',
'219772156',
'219487710',
'334705465',
'336000842',
],
},
];
Expand Down
27 changes: 27 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,33 @@ var validators = {
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': function heIL(str) {
var DNI = /^\d+$/; // sanitize user input

var sanitized = str.trim(); // validate the data structure

if (!DNI.test(sanitized)) {
return false;
}

var id = sanitized;

if (id.length !== 9 || isNaN(id)) {
// Make sure ID is formatted properly
return false;
}

var sum = 0,
incNum;

for (var i = 0; i < id.length; i++) {
incNum = Number(id[i]) * (i % 2 + 1); // Multiply number by 1 or 2

sum += incNum > 9 ? incNum - 9 : incNum; // Sum the digits up and add to total
}

return sum % 10 === 0;
},
'zh-TW': function zhTW(str) {
var ALPHABET_CODES = {
A: 10,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.