This module provides a function to validate UK National Insurance numbers.
It validates strings strictly (must be upper case, contain no spaces, dashes etc).
I recommend santising your input before validation.
npm install is-national-insurance-number --save
const isNino = require('is-national-insurance-number');
isNino('AA111111A'); // true
isNino('thrasdfag'); // false
const sanitiseNino = require('is-national-insurance-number/sanitise-nino');
sanitiseNino('aa 11 22 33 a'); // AA112233A
sanitiseNino('AA-11-22-33-A'); // AA112233A
sanitiseNino('aa112233A! 😺'); // AA112233A
View
<form method="post">
<label for="nino">National Insurance number</label>
<input type="text" name="nino" id="nino"/>
<button type="submit">Submit</button>
</form>
Router
app.post((req, res) => {
const nino = sanitiseNino(req.body.nino);
if (isNino(nino)) {
res.redirect('/good-nino-page');
} else {
const errors = {nino: 'Bad nino!'};
res.render('form', {errors});
}
});
Pull requests welcome! 😸