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

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

提交回复
热议问题