doulin1867 2016-09-28 11:14
浏览 52
已采纳

Wordpress-验证注册字段

I want to check National Code in my registration form, so I create a function for it and create a field for user to write their National Code. But when i check it, it ALWAYS return $javab = Null and user cannot register because of error!

Can you tell me why?
And what should I do to solve this problem?

function checkNationalCode($code = '') {
    $code = (string) preg_replace('/[^0-9]/', '', $code);
    if(strlen($code) > 10 or strlen($code) < 8)
        return false;

    if(strlen($code)==8)
        $code = "00".$code;

    if(strlen($code)==9)
        $code = "0".$code;

    $list_code = str_split($code);
    $last = (int) $list_code[9];
    unset($list_code[9]);
    $i = 10;
    $sum = 0;

    foreach($list_code as $key => $_)
        $sum += intval($_) * $i--;

    $mod = (int) $sum % 11;

    if($mod >= 2)
        $mod = 11 - $mod;

    return true;
}

And Here is my add_action section For creating form :

add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {

    $code_melli = ( ! empty( $_POST['code_melli'] ) ) ? trim( $_POST['code_melli'] ) : '';
    $code = ( ! empty( $_POST['code_melli'] ) ) ? trim( $_POST['code_melli'] ) : '';

        ?>
        <p>
            <label for="code_melli"><?php _e( 'کد ملی :', 'mydomain' ) ?><br />
                <input type="text" name="code_melli" id="code_melli" class="input" value="<?php echo esc_attr( wp_unslash( $code_melli ) ); ?>" size="25" /></label>
        </p>
        <?php
}

I check my fields by sending it to function and returning a boolean variable.

function myplugin_check_fields( $errors, $sanitized_user_login, $user_email ) {

    $javab = checkNationalCode($code);

    if ( $javab == 0 ) {
        $errors->add( 'code_melli_error', __( "<strong>خطا </strong>: کد ملی شما اشتباه میباشد! ", 'mydomain' ) );
    }
    return $errors;
}
  • 写回答

1条回答 默认 最新

  • douqinlin5094 2016-09-28 16:19
    关注

    Solved! By Changing

    $javab = checkNationalCode($code);

    To :

    $javab = checkNationalCode($_POST['code_melli']);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?