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

前端 未结 7 1397
花落未央
花落未央 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: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);
    }
    

提交回复
热议问题