How to validate iranian Melli Code (National Code or Code Melli) in android

前端 未结 7 1350
花落未央
花落未央 2021-02-19 09:25

I need a method to validate the Melli Code (National Code or Code Melli) of iranian people.

I Know it has a formula.

7条回答
  •  不知归路
    2021-02-19 09:56

    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
    

提交回复
热议问题