<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: EdySpider</title>
    <description>The latest articles on DEV Community by EdySpider (@edyspider).</description>
    <link>https://dev.to/edyspider</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F755373%2F5fb702fa-2c7f-4cba-9ce0-19daee3add91.png</url>
      <title>DEV Community: EdySpider</title>
      <link>https://dev.to/edyspider</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edyspider"/>
    <language>en</language>
    <item>
      <title>How to validate a Portuguese NIF in JavaScript (and why I built a package for it)</title>
      <dc:creator>EdySpider</dc:creator>
      <pubDate>Tue, 17 Nov 2026 08:53:51 +0000</pubDate>
      <link>https://dev.to/edyspider/how-to-validate-a-portuguese-nif-in-javascript-and-why-i-built-a-package-for-it-5e7b</link>
      <guid>https://dev.to/edyspider/how-to-validate-a-portuguese-nif-in-javascript-and-why-i-built-a-package-for-it-5e7b</guid>
      <description>&lt;p&gt;If you've ever built a form for a Portuguese user, you've met the &lt;strong&gt;NIF&lt;/strong&gt; (&lt;em&gt;Número de Identificação Fiscal&lt;/em&gt;), the 9-digit tax number that shows up on invoices, sign-up forms, checkouts and HR systems.&lt;/p&gt;

&lt;p&gt;And if you've ever validated it, you've probably done one of two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Checked that it has 9 digits and called it a day, or&lt;/li&gt;
&lt;li&gt;Copied a snippet from a forum, pasted it into a &lt;code&gt;utils.js&lt;/code&gt; and hoped for the best.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I did both, more than once, in different projects. So I turned it into a small npm package: &lt;a href="https://www.npmjs.com/package/@edyspider/validate-nif" rel="noopener noreferrer"&gt;&lt;code&gt;@edyspider/validate-nif&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i @edyspider/validate-nif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;randomNIF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@edyspider/validate-nif'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'123456789'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nf"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'999999999'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nf"&gt;randomNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// e.g. '264586522'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Zero runtime dependencies&lt;/li&gt;
&lt;li&gt;Works with ES modules, CommonJS and TypeScript (types included)&lt;/li&gt;
&lt;li&gt;Runs in Node.js 22.12+, Bun, Deno and the browser&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why "9 digits" is not enough
&lt;/h2&gt;

&lt;p&gt;A NIF is not just any 9-digit number. It has two rules you can check without calling any external service:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The prefix tells you what kind of entity it is.&lt;/strong&gt; A valid NIF starts with:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Prefix&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Individual (singular person)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;45&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-resident individual&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;5&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Company (collective person)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;6&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Public body&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;70&lt;/code&gt;, &lt;code&gt;74&lt;/code&gt;, &lt;code&gt;75&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Inheritance (&lt;em&gt;herança indivisa&lt;/em&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;71&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-resident company&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;72&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Investment funds&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;77&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Temporary assignment by the tax office&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;79&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exceptional regime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sole trader (&lt;em&gt;empresário em nome individual&lt;/em&gt;, no longer issued)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;90&lt;/code&gt;, &lt;code&gt;91&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Condominiums and irregular companies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;98&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non-residents without permanent establishment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;99&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Civil companies without legal personality&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So &lt;code&gt;000000000&lt;/code&gt; or &lt;code&gt;400000000&lt;/code&gt; can never be valid, no matter what.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The last digit is a check digit (mod 11).&lt;/strong&gt; Multiply the first 8 digits by the weights 9, 8, 7, 6, 5, 4, 3, 2, add them up, and take the remainder of dividing by 11:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the remainder is 0 or 1, the check digit is &lt;code&gt;0&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;otherwise, it's &lt;code&gt;11 - remainder&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's &lt;code&gt;123456789&lt;/code&gt; worked out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1×9 + 2×8 + 3×7 + 4×6 + 5×5 + 6×4 + 7×3 + 8×2 = 156
156 mod 11 = 2
11 - 2 = 9  ✅ matches the last digit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In code, that's the whole core of the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;WEIGHTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;computeCheckDigit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;WEIGHTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This catches most typos: a single wrong digit or two swapped neighbours will almost always fail the check. It does &lt;strong&gt;not&lt;/strong&gt; tell you whether the NIF actually belongs to someone. For that you need the tax authority. But it stops obviously broken data before it reaches your database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it in a real form
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;validateNIF()&lt;/code&gt; accepts &lt;code&gt;unknown&lt;/code&gt; and only returns &lt;code&gt;true&lt;/code&gt; for a string in the right format, so you can hand it raw input without guarding first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123456789&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false, numbers are rejected on purpose&lt;/span&gt;
&lt;span class="nf"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nf"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' 123456789 '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false, clean the input first&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Numbers are rejected on purpose: a NIF is an identifier, not a quantity, and treating it as a string avoids surprises with leading zeros or number parsing.&lt;/p&gt;

&lt;p&gt;If you use &lt;a href="https://zod.dev" rel="noopener noreferrer"&gt;Zod&lt;/a&gt;, it plugs straight into a schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'zod'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;validateNIF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@edyspider/validate-nif'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;customerSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;nif&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;validateNIF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Invalid NIF'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Prefer CommonJS? That works too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;validateNIF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'@edyspider/validate-nif'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or straight from a CDN, no bundler needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;validateNIF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'https://cdn.jsdelivr.net/npm/@edyspider/validate-nif@2/+esm'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating test data with &lt;code&gt;randomNIF()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The other half of the package is &lt;code&gt;randomNIF()&lt;/code&gt;, which returns a random &lt;strong&gt;valid&lt;/strong&gt; NIF. I use it for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;seeding a development database;&lt;/li&gt;
&lt;li&gt;unit and end-to-end tests that need a NIF that passes validation;&lt;/li&gt;
&lt;li&gt;demo environments where you don't want real people's tax numbers.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;randomNIF&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'@edyspider/validate-nif'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;fakeCustomer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Test Customer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;nif&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;randomNIF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// passes validateNIF(), belongs to nobody in particular&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It picks from every valid prefix, including the two-digit ones (&lt;code&gt;45&lt;/code&gt;, &lt;code&gt;70&lt;/code&gt;–&lt;code&gt;79&lt;/code&gt;, &lt;code&gt;90&lt;/code&gt;–&lt;code&gt;99&lt;/code&gt;), so your tests cover more than just the common &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed in v2
&lt;/h2&gt;

&lt;p&gt;I first published this package in 2024. Version 2.0.0 is a rewrite to bring it up to date:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Native ES module with a CommonJS entry point&lt;/strong&gt;, so both &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;require()&lt;/code&gt; work&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TypeScript types included&lt;/strong&gt; for both&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No runtime dependencies&lt;/strong&gt;: webpack, Babel and Jest are gone&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tests use the built-in Node.js test runner&lt;/strong&gt; and cover every valid prefix&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Releases are published from GitHub Actions with npm provenance&lt;/strong&gt;, so you can verify the package on npm was built from the public source&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you were on 1.x and used a deep import like &lt;code&gt;@edyspider/validate-nif/dist/bundle.js&lt;/code&gt;, switch to the main entry point. The full list is in the &lt;a href="https://github.com/edyspider/validate-nif/blob/main/CHANGELOG.md" rel="noopener noreferrer"&gt;changelog&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it without installing anything
&lt;/h2&gt;

&lt;p&gt;If you just need to check or generate a NIF right now, there's an online version on my site: &lt;a href="https://edyspider.com/tools/nif" rel="noopener noreferrer"&gt;edyspider.com/tools/nif&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next in this series
&lt;/h2&gt;

&lt;p&gt;The NIF is only one of the Portuguese numbers that forms ask for. Coming up next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NISS&lt;/strong&gt;, the social security number, with &lt;a href="https://www.npmjs.com/package/@edyspider/validate-niss" rel="noopener noreferrer"&gt;&lt;code&gt;@edyspider/validate-niss&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NIB and IBAN&lt;/strong&gt;, plus finding the bank's SWIFT/BIC from the account number, with &lt;a href="https://www.npmjs.com/package/@edyspider/validate-bank-account-pt" rel="noopener noreferrer"&gt;&lt;code&gt;@edyspider/validate-bank-account-pt&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow the series so you don't miss them.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;📦 npm: &lt;a href="https://www.npmjs.com/package/@edyspider/validate-nif" rel="noopener noreferrer"&gt;@edyspider/validate-nif&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 GitHub: &lt;a href="https://github.com/edyspider/validate-nif" rel="noopener noreferrer"&gt;edyspider/validate-nif&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Found a bug or an edge case I missed? &lt;a href="https://github.com/edyspider/validate-nif/issues" rel="noopener noreferrer"&gt;Open an issue&lt;/a&gt; or tell me in the comments. And if this saves you from writing yet another &lt;code&gt;utils/validateNif.js&lt;/code&gt;, a ⭐ on GitHub is always appreciated.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>npm</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
