我有一个可以验证组织/公司编号的JavaScript,但我在C#中需要它。有人躺在附近吗?

这不是一项任务,我可以自己翻译,但是如果已经有人完成,则无需完成工作=)
如果是特定国家/地区,则需要在瑞典使用。

它在javascript中,位于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;
}

提前致谢!

最佳答案

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;
}

测试:
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

07-26 05:52