I have a javascript which validates an organisation/company number but I need it in c#. Does anyone have something like this lying around?
It's not an assignment and I could translate it myself, but if someone's done it already I wouldn't have to go through the work =)
If it's country specific I need to use it in Sweden.
Here it is in javascript, found at http://www.jojoxx.net
function organisationsnummer(nr) {
this.valid = false;
if (!nr.match(/^(\d{1})(\d{5})\-(\d{4})$/))
{
return false;
}
this.group = RegExp.$1;
this.controldigits = RegExp.$3;
this.alldigits = this.group + RegExp.$2 + this.controldigits;
if (this.alldigits.substring(2, 3) < 2)
{
return false
}
var nn = "";
for (var n = 0; n < this.alldigits.length; n++)
{
nn += ((((n + 1) % 2) + 1) * this.alldigits.substring(n, n + 1));
}
this.checksum = 0;
for (var n = 0; n < nn.length; n++)
{
this.checksum += nn.substring(n, n + 1) * 1;
}
this.valid = (this.checksum % 10 == 0) ? true : false;
}
Thanks in advance!
static bool OrganisationsNummer(string nr)
{
Regex rg = new Regex(@"^(\d{1})(\d{5})\-(\d{4})$");
Match matches = rg.Match(nr);
if (!matches.Success)
return false;
string group = matches.Groups[1].Value;
string controlDigits = matches.Groups[3].Value;
string allDigits = group + matches.Groups[2].Value + controlDigits;
if (Int32.Parse(allDigits.Substring(2, 1)) < 2)
return false;
string nn = "";
for (int n = 0; n < allDigits.Length; n++)
{
nn += ((((n + 1) % 2) + 1) * Int32.Parse(allDigits.Substring(n, 1)));
}
int checkSum = 0;
for (int n = 0; n < nn.Length; n++)
{
checkSum += Int32.Parse(nn.Substring(n, 1));
}
return checkSum % 10 == 0 ? true : false;
}
tests:
Console.WriteLine(OrganisationsNummer("556194-7986")); # => True
Console.WriteLine(OrganisationsNummer("802438-3534")); # => True
Console.WriteLine(OrganisationsNummer("262000-0113")); # => True
Console.WriteLine(OrganisationsNummer("14532436-45")); # => False
Console.WriteLine(OrganisationsNummer("1")); # => False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With