gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:07:39 25 4
gpt4 key购买 nike

我有一个 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

关于用于验证组织/公司编号的 c# 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5646375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com