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;
}
Solved! By Changing
$javab = checkNationalCode($code);
To :
$javab = checkNationalCode($_POST['code_melli']);