-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Implement isPassportNumber Validator #1250
Merged
profnandaa
merged 4 commits into
validatorjs:master
from
hamzahejja:add-isPassportNumber-validator
Feb 17, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,12 +35,16 @@ var _isFQDN = _interopRequireDefault(require("./lib/isFQDN")); | |
|
||
var _isBoolean = _interopRequireDefault(require("./lib/isBoolean")); | ||
|
||
var _isLocale = _interopRequireDefault(require("./lib/isLocale")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @profnandaa Remove this ? |
||
|
||
var _isAlpha = _interopRequireWildcard(require("./lib/isAlpha")); | ||
|
||
var _isAlphanumeric = _interopRequireWildcard(require("./lib/isAlphanumeric")); | ||
|
||
var _isNumeric = _interopRequireDefault(require("./lib/isNumeric")); | ||
|
||
var _isPassportNumber = _interopRequireDefault(require("./lib/isPassportNumber")); | ||
|
||
var _isPort = _interopRequireDefault(require("./lib/isPort")); | ||
|
||
var _isLowercase = _interopRequireDefault(require("./lib/isLowercase")); | ||
|
@@ -201,6 +205,7 @@ var validator = { | |
isAlphanumeric: _isAlphanumeric.default, | ||
isAlphanumericLocales: _isAlphanumeric.locales, | ||
isNumeric: _isNumeric.default, | ||
isPassportNumber: _isPassportNumber.default, | ||
isPort: _isPort.default, | ||
isLowercase: _isLowercase.default, | ||
isUppercase: _isUppercase.default, | ||
|
@@ -227,6 +232,7 @@ var validator = { | |
isJSON: _isJSON.default, | ||
isEmpty: _isEmpty.default, | ||
isLength: _isLength.default, | ||
isLocale: _isLocale.default, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @profnandaa Should be removed ? |
||
isByteLength: _isByteLength.default, | ||
isUUID: _isUUID.default, | ||
isMongoId: _isMongoId.default, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = isPassportNumber; | ||
|
||
/** | ||
* Reference: | ||
* https://en.wikipedia.org/ -- Wikipedia | ||
* https://docs.microsoft.com/en-us/microsoft-365/compliance/eu-passport-number -- EU Passport Number | ||
* https://countrycode.org/ -- Country Codes | ||
*/ | ||
var passportRegexByCountryCode = { | ||
AM: /^[A-Z]{2}\d{7}$/, | ||
// ARMENIA | ||
AR: /^[A-Z]{3}\d{6}$/, | ||
// ARGENTINA | ||
AT: /^[A-Z]\d{7}$/, | ||
// AUSTRIA | ||
AU: /^[A-Z]\d{7}$/, | ||
// AUSTRALIA | ||
BE: /^[A-Z]{2}\d{6}$/, | ||
// BELGIUM | ||
BG: /^\d{9}$/, | ||
// BULGARIA | ||
CA: /^[A-Z]{2}\d{6}$/, | ||
// CANADA | ||
CH: /^[A-Z]\d{7}$/, | ||
// SWITZERLAND | ||
CN: /^[GE]\d{8}$/, | ||
// CHINA [G=Ordinary, E=Electronic] followed by 8-digits | ||
CY: /^[A-Z](\d{6}|\d{8})$/, | ||
// CYPRUS | ||
CZ: /^\d{8}$/, | ||
// CZECH REPUBLIC | ||
DE: /^[CFGHJKLMNPRTVWXYZ0-9]{9}$/, | ||
// GERMANY | ||
DK: /^\d{9}$/, | ||
// DENMARK | ||
EE: /^([A-Z]\d{7}|[A-Z]{2}\d{7})$/, | ||
// ESTONIA (K followed by 7-digits), e-passports have 2 UPPERCASE followed by 7 digits | ||
ES: /^[A-Z0-9]{2}([A-Z0-9]?)\d{6}$/, | ||
// SPAIN | ||
FI: /^[A-Z]{2}\d{7}$/, | ||
// FINLAND | ||
FR: /^\d{2}[A-Z]{2}\d{5}$/, | ||
// FRANCE | ||
GB: /^\d{9}$/, | ||
// UNITED KINGDOM | ||
GR: /^[A-Z]{2}\d{7}$/, | ||
// GREECE | ||
HR: /^\d{9}$/, | ||
// CROATIA | ||
HU: /^[A-Z]{2}(\d{6}|\d{7})$/, | ||
// HUNGARY | ||
IE: /^[A-Z0-9]{2}\d{7}$/, | ||
// IRELAND | ||
IS: /^(A)\d{7}$/, | ||
// ICELAND | ||
IT: /^[A-Z0-9]{2}\d{7}$/, | ||
// ITALY | ||
JP: /^[A-Z]{2}\d{7}$/, | ||
// JAPAN | ||
KR: /^[MS]\d{8}$/, | ||
// SOUTH KOREA, REPUBLIC OF KOREA, [S=PS Passports, M=PM Passports] | ||
LT: /^[A-Z0-9]{8}$/, | ||
// LITHUANIA | ||
LU: /^[A-Z0-9]{8}$/, | ||
// LUXEMBURG | ||
LV: /^[A-Z0-9]{2}\d{7}$/, | ||
// LATVIA | ||
MT: /^\d{7}$/, | ||
// MALTA | ||
NL: /^[A-Z]{2}[A-Z0-9]{6}\d$/, | ||
// NETHERLANDS | ||
PO: /^[A-Z]{2}\d{7}$/, | ||
// POLAND | ||
PT: /^[A-Z]\d{6}$/, | ||
// PORTUGAL | ||
RO: /^\d{8,9}$/, | ||
// ROMANIA | ||
SE: /^\d{8}$/, | ||
// SWEDEN | ||
SL: /^(P)[A-Z]\d{7}$/, | ||
// SLOVANIA | ||
SK: /^[0-9A-Z]\d{7}$/, | ||
// SLOVAKIA | ||
TR: /^[A-Z]\d{8}$/, | ||
// TURKEY | ||
UA: /^[A-Z]{2}\d{6}$/, | ||
// UKRAINE | ||
US: /^\d{9}$/ // UNITED STATES | ||
|
||
}; | ||
/** | ||
* Check if str is a valid passport number | ||
* relative to provided ISO Country Code. | ||
* | ||
* @param {string} str | ||
* @param {string} countryCode | ||
* @return {boolean} | ||
*/ | ||
|
||
function isPassportNumber(str, countryCode) { | ||
/** Remove All Whitespaces, Convert to UPPERCASE */ | ||
var normalizedStr = str.replace(/\s/g, '').toUpperCase(); | ||
return countryCode.toUpperCase() in passportRegexByCountryCode && passportRegexByCountryCode[countryCode].test(normalizedStr); | ||
} | ||
|
||
module.exports = exports.default; | ||
module.exports.default = exports.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Reference: | ||
* https://en.wikipedia.org/ -- Wikipedia | ||
* https://docs.microsoft.com/en-us/microsoft-365/compliance/eu-passport-number -- EU Passport Number | ||
* https://countrycode.org/ -- Country Codes | ||
*/ | ||
const passportRegexByCountryCode = { | ||
AM: /^[A-Z]{2}\d{7}$/, // ARMENIA | ||
AR: /^[A-Z]{3}\d{6}$/, // ARGENTINA | ||
AT: /^[A-Z]\d{7}$/, // AUSTRIA | ||
AU: /^[A-Z]\d{7}$/, // AUSTRALIA | ||
BE: /^[A-Z]{2}\d{6}$/, // BELGIUM | ||
BG: /^\d{9}$/, // BULGARIA | ||
CA: /^[A-Z]{2}\d{6}$/, // CANADA | ||
CH: /^[A-Z]\d{7}$/, // SWITZERLAND | ||
CN: /^[GE]\d{8}$/, // CHINA [G=Ordinary, E=Electronic] followed by 8-digits | ||
CY: /^[A-Z](\d{6}|\d{8})$/, // CYPRUS | ||
CZ: /^\d{8}$/, // CZECH REPUBLIC | ||
DE: /^[CFGHJKLMNPRTVWXYZ0-9]{9}$/, // GERMANY | ||
DK: /^\d{9}$/, // DENMARK | ||
EE: /^([A-Z]\d{7}|[A-Z]{2}\d{7})$/, // ESTONIA (K followed by 7-digits), e-passports have 2 UPPERCASE followed by 7 digits | ||
ES: /^[A-Z0-9]{2}([A-Z0-9]?)\d{6}$/, // SPAIN | ||
FI: /^[A-Z]{2}\d{7}$/, // FINLAND | ||
FR: /^\d{2}[A-Z]{2}\d{5}$/, // FRANCE | ||
GB: /^\d{9}$/, // UNITED KINGDOM | ||
GR: /^[A-Z]{2}\d{7}$/, // GREECE | ||
HR: /^\d{9}$/, // CROATIA | ||
HU: /^[A-Z]{2}(\d{6}|\d{7})$/, // HUNGARY | ||
IE: /^[A-Z0-9]{2}\d{7}$/, // IRELAND | ||
IS: /^(A)\d{7}$/, // ICELAND | ||
IT: /^[A-Z0-9]{2}\d{7}$/, // ITALY | ||
JP: /^[A-Z]{2}\d{7}$/, // JAPAN | ||
KR: /^[MS]\d{8}$/, // SOUTH KOREA, REPUBLIC OF KOREA, [S=PS Passports, M=PM Passports] | ||
LT: /^[A-Z0-9]{8}$/, // LITHUANIA | ||
LU: /^[A-Z0-9]{8}$/, // LUXEMBURG | ||
LV: /^[A-Z0-9]{2}\d{7}$/, // LATVIA | ||
MT: /^\d{7}$/, // MALTA | ||
NL: /^[A-Z]{2}[A-Z0-9]{6}\d$/, // NETHERLANDS | ||
PO: /^[A-Z]{2}\d{7}$/, // POLAND | ||
PT: /^[A-Z]\d{6}$/, // PORTUGAL | ||
RO: /^\d{8,9}$/, // ROMANIA | ||
SE: /^\d{8}$/, // SWEDEN | ||
SL: /^(P)[A-Z]\d{7}$/, // SLOVANIA | ||
SK: /^[0-9A-Z]\d{7}$/, // SLOVAKIA | ||
TR: /^[A-Z]\d{8}$/, // TURKEY | ||
UA: /^[A-Z]{2}\d{6}$/, // UKRAINE | ||
US: /^\d{9}$/, // UNITED STATES | ||
}; | ||
|
||
/** | ||
* Check if str is a valid passport number | ||
* relative to provided ISO Country Code. | ||
* | ||
* @param {string} str | ||
* @param {string} countryCode | ||
* @return {boolean} | ||
*/ | ||
export default function isPassportNumber(str, countryCode) { | ||
/** Remove All Whitespaces, Convert to UPPERCASE */ | ||
const normalizedStr = str.replace(/\s/g, '').toUpperCase(); | ||
|
||
return (countryCode.toUpperCase() in passportRegexByCountryCode) && | ||
passportRegexByCountryCode[countryCode].test(normalizedStr); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@profnandaa Could you just update
README.md
at master branch and change/fix this line to the correct styling, i.e.**isPassportNumber(str, countryCode)**
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, will do that.