lux-utils
Version:
JS library to validate and format common Luxembourgish administrative data.
114 lines (88 loc) • 5.15 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>validateLicensePlate/index.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="LuxUtils.html">LuxUtils</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateCodePostal">validateCodePostal</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateFixedPhone">validateFixedPhone</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateFixedPhoneWithPrefix">validateFixedPhoneWithPrefix</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateLicensePlate">validateLicensePlate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateMatricule">validateMatricule</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateMobilePhone">validateMobilePhone</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validateMobilePhoneWithPrefix">validateMobilePhoneWithPrefix</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validatePhone">validatePhone</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="LuxUtils.html#.validatePhoneWithPrefix">validatePhoneWithPrefix</a></span></li>
</nav>
<div id="main">
<h1 class="page-title">validateLicensePlate/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @memberof LuxUtils
* @function validateLicensePlate
* @description Returns `true` if the given postal code is valid
* @param {(string|integer)} licensePlateToValidate postal code to validate. Can be a `string` or a `integer`
* @returns {boolean} `true` or `false`
*
* @example
* // validate a postal code
* let isValid = LuxUtils.validateLicensePlate(4402); //true;
* let isValid2 = LuxUtils.validateLicensePlate("L-4402"); //true;
* let isValid3 = LuxUtils.validateLicensePlate("0404"); //false;
*/
export default (licensePlateToValidate) => {
// format: custom plate - 1111 or 11111
if (Number.isInteger(licensePlateToValidate)) {
return (
licensePlateToValidate.toString().length === 4
|| licensePlateToValidate.toString().length === 5
);
}
// format: LL 22
// format: LL 1234
if (typeof licensePlateToValidate === 'string') {
const strippedLicensePlateToValidate = licensePlateToValidate.replace(/\s/g, '');
const twoFirstLetters = strippedLicensePlateToValidate.substr(0, 2);
if (isTwoLetters(twoFirstLetters)) {
const rest = strippedLicensePlateToValidate.substr(
2,
strippedLicensePlateToValidate.length - 2,
);
if (rest.length === 4 && /^\d+$/.test(rest)) {
return true;
}
} else {
// custom plate
return (
strippedLicensePlateToValidate.toString().length === 4
|| strippedLicensePlateToValidate.toString().length === 5
);
}
}
return false;
};
function isTwoLetters(str) {
const regex = /^[a-z]+$/i;
return str.length === 2 && regex.test(str);
}
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Feb 18 2019 12:36:27 GMT+0000 (WET) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>