ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

IsValidCodeMelli

صحت کد ملی

Source

public static bool IsValidCodeMelli(this string codeMelli)
{
    Int64 number;
    int sum = 0, temp;
    Int64.TryParse(codeMelli, out number);
    if (Math.Log10(number) > 6 && Math.Log10(number) < 10)
    {
        temp = Convert.ToInt16(number % 10);
        number /= 10;
        for (int i = 2; i < 11; i++)
        {
            sum += Convert.ToInt16(i * (number % 10));
            number /= 10;
        }
        if (((sum % 11 < 2) && (sum % 11 == temp)) || ((sum % 11 >= 2) && ((11 - sum % 11) == temp)))
            return true;
    }
    return false;
}

Example

string codeMelli = "4990003519";
bool isValid = codeMelli.IsValidCodeMelli();

Author: Mehdi Miri

Submitted on: 26 mrt 2013

Language: C#

Type: System.String

Views: 4510