Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate Vat number in C#
    primarykey
    data
    text
    <p>VAT numbers are not randomly or sequentially generated but are based on a formula that can be checked to see if the number is valid. If a VAT number is invalid a business cannot re-claim the VAT.</p> <p>To validate a UK VAT number manually you can perform the following exercise:</p> <p>Excluding the first 2 letters, list the numbers vertically and multiply each by a value starting with 8 and ending with 2. Then add up all the sums you have and deduct 97 from the sum until the answer is negative. The negative sum should be equal to the last 2 digits of the VAT number. </p> <p>So for example, the VAT number for <code>BLABLA</code> is GB 815382334 the calculation is:</p> <pre><code>8 x 8 = 64 1 x 7 = 7 5 x 6 = 30 3 x 5 = 15 8 x 4 = 32 2 x 3 = 6 3 x 2 = 6 </code></pre> <p>The total of the above calculation is <code>64 + 7 + 30 + 15 + 32 + 6 + 6 = 160</code> Deduct 97 from this until the result is negative, the result is <code>160 – 97 - 97= -34</code> which is the same as the last two digits: so the VAT number is valid.</p> <p>I want to write a C# application that takes a UK VAT Number as input, calculates the checksum using the above formula, and indicates if the number is valid or invalid.</p> <p>This is for me an exercise in algorithm's. I have found vat checkers online but I don't understand how they are working, so I was hoping some one could give some simple answers to the above problem with good explanations?</p> <p><strong>Update</strong>:</p> <pre><code> public static bool isValidVATNumber(string theVATNumber) { string startChar = "^"; string endChar = "$"; bool rtn = false; int i = 8; string valString; int sum = 0; // Check that the string matches the requirements rtn = Regex.IsMatch(theVATNumber, (startChar + ("(([1-9]d{8})|([1-9]d{11}))" + endChar)), RegexOptions.Multiline); if (rtn) { // Perform the validation valString = theVATNumber; if (Regex.IsMatch(valString, (startChar + "[A-Z]{2}"), RegexOptions.Multiline)) { valString = valString.Substring(2); } while ((i &gt;= 2)) { sum = (sum + (i * int.Parse(valString.Substring(0, 1)))); valString = valString.Substring(1); i--; } while ((sum &gt; 0)) { sum -= 97; } rtn = ((sum * -1) == int.Parse(valString)); } return rtn; } </code></pre> <p>Note the above method does not work and for me more difficult to understand, I started with my own method that I find much easier to work with but yet to finish it (please note it is embarrassing)</p> <pre><code> List&lt;int&gt; integerList = new List&lt;int&gt;(); int b = 8; for (int a = 0; a &lt; textBox1.Text.Length; a++) { integerList.Add(int.Parse(textBox1.Text[a].ToString())); } foreach (int item in integerList) { listBox1.Items.Add(item * b); --b; } </code></pre> <p>I still have to take the sum of the list and do the rest of the calculations and was hoping to pick some peoples brains as to other ways it might be done (easier ways).</p> <p>Update on my own method and abit of thanks to Pax below:</p> <pre><code> List&lt;int&gt; integerList = new List&lt;int&gt;(); List&lt;int&gt; sumList = new List&lt;int&gt;(); int b = 8; // Will be 8 for the first multiplication. for (int a = 0; a &lt;= 6; a++) { integerList.Add(int.Parse(textBox1.Text[a].ToString())); } foreach (int item in integerList) // Loop once per input digit. { //listBox1.Items.Add(item * b); sumList.Add(item * b); --b; } listBox1.DataSource = sumList; int sum = sumList.Sum(); while (sum &gt; 0) { sum = sum - 97; } int myInt = System.Math.Abs(sum); label1.Text = Convert.ToString(myInt); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload