如何在 cakephp 中验证个人身份证号码

How validate a personal identification number in cakephp

我需要验证我的表单中的一个字段,这个字段属于我所在国家/地区的个人身份证号码,这个号码有 10 位数字

例子:卡=1710034065

2 1 2 1 2 1 2 1 2(系数) 1 7 1 0 0 3 4 0 6(个人身份证号码) 2 7 2 0 0 3 8 0 12 = 25(将个人号码的每位数字乘以 3系数,如果结果> 10在数字之间添加)。

加乘法

求和结果

25/10 = 2,余数5,除以10 - 余数5 = 5(校验位)**等于身份号码的最后一位数**


现在我需要在框架中实现这个逻辑,但我不知道如何实现, 我在 java 中有一个示例代码,可以更好地了解我需要做什么。

    function check_cedula( form )
{
  var cedula = form.cedula.value;
  array = cedula.split( "" );
  num = array.length;
  if ( num == 10 )
  {
    total = 0;
    digito = (array[9]*1);
    for( i=0; i < (num-1); i++ )
    {
      mult = 0;
      if ( ( i%2 ) != 0 ) {
        total = total + ( array[i] * 1 );
      }
      else
      {
        mult = array[i] * 2;
        if ( mult > 9 )
          total = total + ( mult - 9 );
        else
          total = total + mult;
      }
    }
    decena = total / 10;
    decena = Math.floor( decena );
    decena = ( decena + 1 ) * 10;
    final = ( decena - total );
    if ( ( final == 10 && digito == 0 ) || ( final == digito ) ) {
      alert( "La c\xe9dula ES v\xe1lida!!!" );
      return true;
    }
    else
    {
      alert( "La c\xe9dula NO es v\xe1lida!!!" );
      return false;
    }
  }
  else
  {
    alert("La c\xe9dula no puede tener menos de 10 d\xedgitos");
    return false;
  }
}

假设您的型号名称是 User,数据库中的字段是 card,您将执行以下操作;

<?php
class User extends AppModel {

    /**
     * Validation rules
     */
     public $validate = array(
        'card' => array(
            'validateCard' => array(
                'rule' => array('validateCard'),
                'message' => 'Card does not validate'
            )
        )
    );

    /**
     * Custom validation rule
     * @return bool
     */
    public function validateCard($field) {
        $cardNumber = $field['card'];

        // Here, perform your logic and return a boolean

    }


}

此外,请确保在您看来,您正在使用 FormHelper 输出表单输入,并且一切都应该正常运行。例如;

<?php
echo $this->Form->create();
echo $this->Form->input('User.card');
echo $this->Form->end();