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

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

    For Javascript Developers:

    const checkCodeMeli = code => {
        console.log("an");
        var L = code.length;
    
        if (L < 8 || parseInt(code, 10) === 0) return false;
        code = ("0000" + code).substr(L + 4 - 10);
        if (parseInt(code.substr(3, 6), 10) === 0) return false;
        var c = parseInt(code.substr(9, 1), 10);
        var s = 0;
        for (var i = 0; i < 9; i++) {
          s += parseInt(code.substr(i, 1), 10) * (10 - i);
        }
        s = s % 11;
        return (s < 2 && c === s) || (s >= 2 && c === 11 - s);
      };

    0 讨论(0)
  • 2021-02-19 09:56

    I have so many different versions of it here but the following is its Kotlin version,

    fun isValidIranianNationalCode(input: String): Boolean {
      if (!"^\\d{10}$".toRegex().matches(input))
        return false
    
      input.toCharArray().map(Character::getNumericValue).let {
        val check = it[9]
        val sum = (0..8).map { i -> it[i] * (10 - i) }.sum() % 11
        return sum < 2 && check == sum || sum >= 2 && check + sum == 11
      }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-19 10:00

    Good job Alireza, Here is my code which is very similar to yours.

    ‍‍‍

    private boolean isValidNationalCode(String nationalCode) {
        if (nationalCode.length() != 10) {
          return false;
        } else {
          //Check for equal numbers
          String[] allDigitEqual = {"0000000000", "1111111111", "2222222222", "3333333333",
              "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999"};
          if (Arrays.asList(allDigitEqual).contains(nationalCode)) {
            return false;
          } else {
            int sum = 0;
            int lenght = 10;
            for (int i = 0; i < lenght - 1; i++) {
              sum += Integer.parseInt(String.valueOf(nationalCode.charAt(i))) * (lenght - i);
            }
    
            int r = Integer.parseInt(String.valueOf(nationalCode.charAt(9)));
    
            int c = sum % 11;
    
            return (((c < 2) && (r == c)) || ((c >= 2) && ((11 - c) == r)));
          }
    
        }
      }
    
    0 讨论(0)
  • 2021-02-19 10:06

    Java 8

    import java.util.stream.Streams;
    import java.util.function.IntUnaryOperator;
    public static boolean isValidIranianNationalCode(String input) {
    if (!input.matches("^\\d{10}$"))
        return false;
    
    int check = Integer.parseInt(input.substring(9, 10));
    
    int sum = IntStream.range(0, 9)
            .map(x -> Integer.parseInt(input.substring(x, x + 1)) * (10 - x))
            .sum() % 11;
    
    return (sum < 2 && check == sum) || (sum >= 2 && check + sum == 11);
    }
    
    0 讨论(0)
  • 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 ));
    }
    
    0 讨论(0)
提交回复
热议问题