Skip to content

Instantly share code, notes, and snippets.

@vipickering
Created October 12, 2016 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vipickering/a26d57516bd402e5c01c3bf27cdeac41 to your computer and use it in GitHub Desktop.
Save vipickering/a26d57516bd402e5c01c3bf27cdeac41 to your computer and use it in GitHub Desktop.
Validate NINO Functions (Node)
// National Insurance Validation Module.
// Sanitise the NINO by first passing through this function, which will strip out space and dashes, then convert to uppercase.
exports.sanitise = function sanitise(nino) {
return nino.toUpperCase().replace(/[\s|\-]/g, '');
};
// You can make the last letter optional by using this function.
exports.validateLoose = function validateLoose(nino) {
var regex = /^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{0,1}$/;
return regex.test(nino);
};
// You can make the last letter mandatory by using this function.
exports.validateStrict = function validateStrict(nino) {
var regex = /^(?!BG|GB|NK|KN|TN|NT|ZZ)[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$/;
return regex.test(nino);
};
var ninoChecker = require(appRootDirectory + '/app/functions/ninoCheck');
function (req, res) {
var ninoRaw = req.body.ninoField; //Get the value from the input and assign to a var.
var sanitisedNino = ninoChecker.sanitise(ninoRaw); // Run it through the optional sanitiser
//Finally attempt to validate the NINO using either (1) or (2).
var convertedNino = ninoChecker.validateStrict(sanitisedNino); //(1)
var convertedNino = ninoChecker.validateLoose(sanitisedNino); //(2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment