title: Can this function be rewritten with a regex?
body: <p>I want to reformat and validate if a user has provided a valid Belgian enterprise number. Because the input can be all of the following examples:</p>
  
  <ul>
  <li>BE 0123.321.123</li>
  <li>BE0123.321.123</li>
  <li>BE0123   321 123</li>
  <li>0123.321.123</li>
  <li>123.321.123</li>
  <li>123321123</li>
  </ul>
  
  <p>I've written a function that validates and reformat the input to a 'display' version (BE 0123.123.123) and a 'code' version (123123123). This function looks like this.</p>
  
  <pre><code>formatAndValidateEnterpriseNumber = enterpriseNumber =&gt; {
      if(enterpriseNumber === undefined || !enterpriseNumber || (enterpriseNumber || '').length &lt; 3) return { isValid: false, error: 'Please fill in your enterprise number' };
  
          //Remove space, dots, ...
          enterpriseNumber = enterpriseNumber.toUpperCase();
          enterpriseNumber = enterpriseNumber.replace(/[. ,:-]+/g, '');
  
          //Check for double country code
          const reDouble = /^[a-zA-Z]{4}/;
          if (reDouble.test(enterpriseNumber)) enterpriseNumber = enterpriseNumber.substring(2);
  
          if (enterpriseNumber.length &lt; 9 || enterpriseNumber.length &gt; 12) return { isValid: false, error: 'The length of the provided number is incorrect' };
  
          //Check country code
          const reBE = /^[a-zA-Z]{2}/;
          if (reBE.test(enterpriseNumber)) {
              //Check if country code = BE
              if (enterpriseNumber.slice(0, 2) !== 'BE') return { isValid: false, error: 'Please fill in a Belgian enterprise number' };
              // Remove country code
              else enterpriseNumber = enterpriseNumber.substring(2);
          }
  
          //Check if first digit is 0
          if (enterpriseNumber.length === 10 &amp;&amp; enterpriseNumber.startsWith('0')) enterpriseNumber = enterpriseNumber.substring(1);
  
          //Check if enterpriseNumber is valid with modulo test
          if (parseInt(97 - (enterpriseNumber.slice(0, 7) % 97), 10) !== parseInt(enterpriseNumber.slice(7, 9), 10))
              return { isValid: false, error: 'The provided number is invalid'}
  
        return {
              isValid: true,
              enterpriseNumber: enterpriseNumber,
              displayEnterpriseNumber: `BE 0${enterpriseNumber.substring(0, 3)}.${enterpriseNumber.substring(3, 6)}.${enterpriseNumber.substring(6, 9)}`
        };
  };
  </code></pre>
  
  <p>I think it's pretty messy and I'm wondering if this can be improved with one/two regex tests that reformat and validate the user's input?</p>
  
  <p>A second question: Sometimes for account or credit cards numbers the input field had those underscores and lines (-) already in the input box and reformat the number while typing. What is this method called and can this be done for a specific thing like a Belgian enterprise number? </p>