I need a method to validate the Melli Code (National Code or Code Melli) of iranian people.
I Know it has a formula.
First, use the System.Text.RegularExpressions
class and then use this script:.
#region NationalCode Check
public bool NationalCodeCheck(string input)
{
if (!Regex.IsMatch(input, @"^\d{10}$"))
return false;
var check = Convert.ToInt32(input.Substring(9, 1));
var sum = Enumerable.Range(0, 9)
.Select(x => Convert.ToInt32(input.Substring(x, 1)) * (10 - x))
.Sum() % 11;
return (sum < 2 && check == sum) || (sum >= 2 && check + sum == 11);
}
#endregion