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

时间:2011-04-13 08:24:54

标签: c# javascript 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;
}  

提前致谢!

3 个答案:

答案 0 :(得分: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;
}

测试:

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

答案 1 :(得分:2)

有一个在线JavaScript to C# converter

可能有tons of others,可能有一个至少适合帮助您完成部分转换。

另一方面,手动转换这几行似乎相当简单。

答案 2 :(得分:0)

更扎实的实施

/// <summary>
/// Immutable value type for storing personal numbers
/// </summary>
public struct PersonalNumber : IEquatable<PersonalNumber>
{
    private const string YearHundredOpt = @"(19|20)?"; // 19 or 20 only (optional)
    private const string YearDecade = @"(\d{2})"; // any two digits
    private const string Month = @"(0[1-9]|10|11|12)"; // 01 to 12
    private const string Day = @"(0[1-9]|1\d|2\d|30|31)"; // 01 to 31
    private const string SeparatorAndLastFourOpt = @"([-\+]?)(\d{4})?"; // optional - or + followed by any four digits (optional)

    private const string RegExForValidation =
        "^" + YearHundredOpt + YearDecade + Month + Day + SeparatorAndLastFourOpt + "$";

    private static readonly Regex personNoRegex = new Regex(RegExForValidation, RegexOptions.Compiled);

    private readonly string rawPersonalNumber;
    private readonly string personalNumber;
    private readonly bool isValid;
    private readonly bool isPlus100YearsOld;

    public PersonalNumber(string personalNumber)
    {
        rawPersonalNumber = personalNumber;

        var matches = personNoRegex.Matches(personalNumber);
        var normalizedYYMMDDXXXC = string.Empty;
        isPlus100YearsOld = false;
        foreach (Match match in matches)
        {
            if (match.Success)
            {
                normalizedYYMMDDXXXC = match.Groups[2].Value + match.Groups[3].Value +
                              match.Groups[4].Value + match.Groups[6].Value;
                if (match.Groups[5].Success)
                {
                    isPlus100YearsOld = match.Groups[5].Value == "+";
                }
                break;
            }
        }

        if (normalizedYYMMDDXXXC.Length > 6)
        {
            isValid = personNoRegex.IsMatch(personalNumber) && LuhnAlgorithm.ValidateChecksum(normalizedYYMMDDXXXC);
        }
        else
        {
            isValid = personNoRegex.IsMatch(personalNumber);
        }

        this.personalNumber = string.IsNullOrEmpty(normalizedYYMMDDXXXC) ? rawPersonalNumber : normalizedYYMMDDXXXC;
    }

    public string Number { get { return personalNumber; } }
    public bool IsPlus100YearsOld { get { return isPlus100YearsOld; } }
    public bool IsValid { get { return isValid; } }
    public bool IsMale { get { return !IsFemale; } }

    public bool IsFemale
    {
        get
        {
            var genderCharacter = personalNumber.Substring(personalNumber.Length - 2, 1); // second to last character
            return int.Parse(genderCharacter) % 2 == 0; // even = female, odd = male
        }
    }

    public override string ToString() { return Number; }

    public bool Equals(PersonalNumber other)
    {
        return Equals(other.personalNumber, personalNumber);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (obj.GetType() != typeof (PersonalNumber)) return false;
        return Equals((PersonalNumber) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (rawPersonalNumber != null ? rawPersonalNumber.GetHashCode() : 0);
            result = (result*397) ^ (personalNumber != null ? personalNumber.GetHashCode() : 0);
            result = (result*397) ^ isValid.GetHashCode();
            return result;
        }
    }

    public static bool operator ==(PersonalNumber left, PersonalNumber right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(PersonalNumber left, PersonalNumber right)
    {
        return !left.Equals(right);
    }
}

和一些测试

[TestClass]
public class PersonalNumberFixture
{
    [TestMethod]
    public void Ctor_PersonalNumber_AsUnformatted()
    {
        AssertExpectedNumber("7102240475", "7102240475");
        AssertExpectedNumber("197102240475", "7102240475");
        AssertExpectedNumber("19710224-0475", "7102240475");
        AssertExpectedNumber("710224-0475", "7102240475");
    }

    private static void AssertExpectedNumber(string number, string expectedNumber)
    {
        Assert.AreEqual(expectedNumber, new PersonalNumber(number).Number);
    }

    [TestMethod]
    public void FiltersAndRemovesPlusAndMinusCharactersCorrectly()
    {
        Assert.AreEqual("7102240475", new PersonalNumber("710224-0475").Number);
        Assert.AreEqual("7102240475", new PersonalNumber("710224+0475").Number);

        Assert.AreEqual("7102240475", new PersonalNumber("19710224-0475").Number);
        Assert.AreEqual("7102240475", new PersonalNumber("19710224+0475").Number);
    }

    [TestMethod]
    public void PlusAndMinusCharactersOnlyAllowedInCertainPositions()
    {
        Assert.IsTrue(new PersonalNumber("710224-0475").IsValid);
        Assert.IsTrue(new PersonalNumber("710224+0475").IsValid);
        Assert.IsFalse(new PersonalNumber("71022404-75").IsValid);
        Assert.IsFalse(new PersonalNumber("71022404+75").IsValid);
        Assert.IsFalse(new PersonalNumber("710-224-0475").IsValid);
        Assert.IsFalse(new PersonalNumber("710224+04+75").IsValid);
    }

    [TestMethod]
    public void MaleAndFemale_CorrectlyIdentified()
    {
        // female
        Assert.IsFalse(new PersonalNumber("551213-7986").IsMale);
        Assert.IsTrue(new PersonalNumber("551213-7986").IsFemale);

        // male
        Assert.IsTrue(new PersonalNumber("710224-0475").IsMale);
        Assert.IsFalse(new PersonalNumber("710224-0475").IsFemale);
        Assert.IsTrue(new PersonalNumber("19710224-0475").IsMale);
        Assert.IsFalse(new PersonalNumber("19710224-0475").IsFemale);
    }

    [TestMethod]
    public void Ctor_ValidPersonalNumbers_ParsedAsValid()
    {
        TestLoop_ValidPersonalNumbers(new[] { "7102240475", "197102240475", "19710224-0475", "710224-0475", "801231+3535", "630215" });
    }

    private static void TestLoop_ValidPersonalNumbers(IEnumerable<string> personalNumbers)
    {
        foreach (var personalNumber in personalNumbers)
        {
            var pn = new PersonalNumber(personalNumber);

            Assert.IsTrue(pn.IsValid, string.Format("Pno = '{0}'", personalNumber));
        }
    }

    [TestMethod]
    public void ToString_EqualsNormalizedNumber()
    {
        Assert.AreEqual(new PersonalNumber("460126").ToString(), new PersonalNumber("460126").Number);
    }

    [TestMethod]
    public void Ctor_InvalidPersonalNumbers_ParsedAsNotValid()
    {
        TestLoop_InvalidPersonalNumbers(new[] { "127102240475", "19710XY40475", "19710224=0475", "1971", "14532436-45", "556194-7986", "262000-0113", "460531-12" });
    }

    private static void TestLoop_InvalidPersonalNumbers(IEnumerable<string> personalNumbers)
    {
        foreach (var personalNumber in personalNumbers)
        {
            var pn = new PersonalNumber(personalNumber);

            Assert.IsFalse(pn.IsValid, string.Format("Pno = '{0}'", personalNumber));
        }
    }

    [TestMethod]
    public void TwoNumbers_ConsideredEqual_IfNormalizedNumbersAreEqual()
    {
        new PersonalNumber("710224-0475");
        Assert.IsTrue(new PersonalNumber("800212").Equals(new PersonalNumber("19800212")));
        Assert.IsTrue(new PersonalNumber("800212").Equals(new PersonalNumber("800212")));
        Assert.IsTrue(new PersonalNumber("8002121234").Equals(new PersonalNumber("19800212-1234")));

        Assert.IsFalse(new PersonalNumber("800212").Equals(new PersonalNumber("900212")));
    }

    [TestMethod]
    public void EqualsOperatorOverloaded_AsEquals()
    {
        Assert.IsTrue(new PersonalNumber("800212") == new PersonalNumber("19800212"));
        Assert.IsTrue(new PersonalNumber("800212") == new PersonalNumber("800212"));
        Assert.IsTrue(new PersonalNumber("8002121234") == new PersonalNumber("19800212-1234"));

        Assert.IsTrue(new PersonalNumber("800212") != new PersonalNumber("900212"));
    }

    [TestMethod]
    public void IsPlus100YearsOld()
    {
        Assert.IsFalse(new PersonalNumber("800213").IsPlus100YearsOld);
        Assert.IsFalse(new PersonalNumber("800213-0123").IsPlus100YearsOld);

        Assert.IsTrue(new PersonalNumber("800213+0123").IsPlus100YearsOld);
    }
}