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']);

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

报告相同问题?

悬赏问题

  • ¥15 关于cpci总线的几个问题,有点迷糊
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护