在jQuery中检查Persian Melli代码

时间:2017-10-24 08:38:05

标签: javascript jquery

如何在jQuery中检查波斯语Melli代码 我用c#语言编写了这段代码并且工作正常但是当我将它转换为jQuery时没有工作。 我写了这段代码但不起作用:

function checkCodeMeli(obj) {
  var code = obj.value;
  var L = code.length;

  if (L < 8 || parseInt(code, 10) == 0) {
    $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
    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 - 1) * (10 - i);
  s = s % 11;
  var t = (s < 2 && c == s) || (s >= 2 && c == (11 - s))
  ;
  if (t == true)
    $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
  else
    $('#' + obj.id).css("background-color", "#fff").css("border", "1px solid gray");

  return t;
  return true;
}

5 个答案:

答案 0 :(得分:1)

您好amir您可以使用此代码检查伊朗国家代码:

    function checkCodeMeli(obj) {
var input=obj.value;
if (!/^\d{10}$/.test(input))
   {
       $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
   return false;
   } 

var check = parseInt(input[9]);
var sum = 0;
var i;
for (i = 0; i < 9; ++i) {
    sum += parseInt(input[i]) * (10 - i);
}
sum %= 11;

var isValid= (sum < 2 && check == sum) || (sum >= 2 && check + sum == 11);
if(!isValid){
    $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
}else{
    $('#' + obj.id).css("background-color", "#38d043").css("border", "1px solid black");
}
return isValid;}     

答案 1 :(得分:1)

JavaScript中的

&#13;
&#13;
console.log("1377456935 valid Code: "+checkMelliCode('1377456935'));
console.log("1377456938 Invalid Code: "+checkMelliCode('1377456938'));

function checkMelliCode(meli_code) {
  
if (meli_code.length == 10) {
  if (meli_code == '1111111111' || meli_code == '0000000000' || meli_code == '2222222222' || meli_code == '3333333333' || meli_code == '4444444444' || meli_code == '5555555555' || meli_code == '6666666666' || meli_code == '7777777777' || meli_code == '8888888888' || meli_code == '9999999999') {
           return false;
       }
       c = parseInt(meli_code.charAt(9));
       n = parseInt(meli_code.charAt(0)) * 10 + parseInt(meli_code.charAt(1)) * 9 + parseInt(meli_code.charAt(2)) * 8 + parseInt(meli_code.charAt(3)) * 7 + parseInt(meli_code.charAt(4)) * 6 + parseInt(meli_code.charAt(5)) * 5 + parseInt(meli_code.charAt(6)) * 4 + parseInt(meli_code.charAt(7)) * 3 + parseInt(meli_code.charAt(8)) * 2;
       r = n - parseInt(n / 11) * 11;
       if ((r == 0 && r == c) || (r == 1 && c == 1) || (r > 1 && c == 11 - r)) {
           return true;
       } else {
           return false;
       }
   } else {
       return false;
   }
}
&#13;
&#13;
&#13;

在C#中

public bool IsValidNationalCode(string nationalcode)
{
    if (string.IsNullOrEmpty(nationalcode)) return false;
    if (!new Regex(@"\d{10}").IsMatch(nationalcode)) return false;
    var array = nationalcode.ToCharArray();
    var allDigitEqual = new[] { "0000000000", "1111111111", "2222222222", "3333333333", "4444444444", "5555555555", "6666666666", "7777777777", "8888888888", "9999999999" };
    if (allDigitEqual.Contains(nationalcode)) return false;
    var j = 10;
    var sum = 0;
    for (var i = 0; i < array.Length - 1; i++)
    {
        sum += Int32.Parse(array[i].ToString(CultureInfo.InvariantCulture)) * j;
        j--;
    }
    var div = sum / 11;
    var r = div * 11;
    var diff = Math.Abs(sum - r);
    if (diff <= 2)
    {
        return diff == Int32.Parse(array[9].ToString(CultureInfo.InvariantCulture));
    }
    var temp = Math.Abs(diff - 11);
    return temp == Int32.Parse(array[9].ToString(CultureInfo.InvariantCulture));
}

答案 2 :(得分:0)

检查此代码(JavaScript)

console.log("1377456935 valid Code: "+checkMeliCode('1377456935'));
console.log("1377456938 Invalid Code: "+checkMeliCode('1377456938'));

function checkMeliCode(code) {
    if (!/^\d{8,10}$/.test(code) || /^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/.test(code))
        return false;
    var L = code.length,
        _ = 0;
    for (i = 0; i < L - 1; i++)
        _ += code.charAt(i) * (L - i);
    _ %= 11;
    return (code.charAt(L - 1) == ((_ < 2) ? _ : 11 - _))
}

答案 3 :(得分:0)

这解决了我的问题:

function checkCodeMeli(obj) {

    var code = obj.value;
    var L = code.length;
    if (L < 8 || parseInt(code, 10) == 0) {
        $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
        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;
    var t = (s < 2 && c == s) || (s >= 2 && c == (11 - s));
    if (t == false)
        $('#' + obj.id).css("background-color", "#f8e8e8").css("border", "1px solid red");
    else
        $('#' + obj.id).css("background-color", "#fff").css("border", "1px solid gray");
    return t;
    return true;
}

答案 4 :(得分:0)

尝试 Persian-tools 一个很棒的 JavaScript 库以及许多其他有用的功能。

import { verifyIranianNationalId, getPlaceByIranNationalId } from "persian-tools2";

verifyIranianNationalId("0067749828"); // true
verifyIranianNationalId("0684159415"); // false