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

前端 未结 7 1349
花落未央
花落未央 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 10:10

    You can validate your national code like this:

    Updated

    public static boolean isValidNationalCode(String nationalCode)
    {
          if (!nationalCode.matches("^\\d{10}$"))
              return false;
    
          int sum = 0;
    
          for (int i = 0; i < 9; i++)
          {
              sum += Character.getNumericValue(nationalCode.charAt(i)) * (10 - i);
          }
    
          int lastDigit = Integer.parseInt(String.valueOf(nationalCode.charAt(9)));
          int divideRemaining = sum % 11;
    
          return ((divideRemaining < 2 && lastDigit == divideRemaining) ||   (divideRemaining >= 2 && (11 - divideRemaining) == lastDigit ));
    }
    

提交回复
热议问题