用于验证组织/公司编号的c#代码?

Nik*_*las 4 javascript c# regex validation

我有一个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;
}  
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Vas*_*ich 7

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;
}
Run Code Online (Sandbox Code Playgroud)

测试:

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
Run Code Online (Sandbox Code Playgroud)