Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Persian Melli code in jQuery

How can I check Persian Melli code in jQuery I have this code in c# language and work properly but when I converted this to jQuery didn't work. I wrote this code but does not work :

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;
}
like image 374
Amir H KH Avatar asked Jan 01 '26 10:01

Amir H KH


1 Answers

Hi amir you can use this code to check Iranian national code:

    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;}     
like image 134
Alireza Avatar answered Jan 05 '26 20:01

Alireza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!