Ultimate Member 插件登录页面添加自定义验证功能

以增加 ABN码自定义验证为例;

在 wordpress 主题根目录下的 functions.php 中添加如下代码;

//验证ABN


add_action('um_submit_form_errors_hook_','um_custom_validate_abn', 999, 1);
function um_custom_validate_abn( $args ) {
    global $ultimatemember;

    if ( isset( $args['abn'] ) && !isValidAbnOrAcn($args['abn']) ) {
        $ultimatemember->form->add_error( 'abn', 'Abn code format error' );
    }
    /*$args 为data-key值*/
}
/**
 * Return true if $number is a valid ABN
 * @param string $number
 * @return bool True if $number is a valid ABN
 */
function isValidAbnOrAcn($number) {
    $number = preg_replace('/[^0-9]/', '', $number);
    if(strlen($number) == 9) {
        return isValidAcn($number);
    }
    if(strlen($number) == 11) {
        return isValidAbn($number);
    }
    return false;
}
/**
 * Validate an Australian Business Number (ABN)
 * @param string $abn
 * @link http://www.ato.gov.au/businesses/content.asp?doc=/content/13187.htm
 * @return bool True if $abn is a valid ABN, false otherwise
 */
function isValidAbn($abn) {
    $weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
    // Strip non-numbers from the acn
    $abn = preg_replace('/[^0-9]/', '', $abn);
    // Check abn is 11 chars long
    if(strlen($abn) != 11) {
        return false;
    }
    // Subtract one from first digit
    $abn[0] = ((int)$abn[0] - 1);
    // Sum the products
    $sum = 0;
    foreach(str_split($abn) as $key => $digit) {
        $sum += ($digit * $weights[$key]);
    }
    if(($sum % 89) != 0) {
        return false;
    }
    return true;
}
/**
 * Validate an Australian Company Number (ACN)
 * @param string $acn
 * @link http://www.asic.gov.au/asic/asic.nsf/byheadline/Australian+Company+Number+(ACN)+Check+Digit
 * @return bool True if $acn is a valid ACN, false otherwise
 */
function isValidAcn($acn){
    $weights = array(8, 7, 6, 5, 4, 3, 2, 1, 0);
    // Strip non-numbers from the acn
    $acn = preg_replace('/[^0-9]/', '', $acn);
    // Check acn is 9 chars long
    if(strlen($acn) != 9) {
        return false;
    }
    // Sum the products
    $sum = 0;
    foreach(str_split($acn) as $key => $digit) {
        $sum += $digit * $weights[$key];
    }
    // Get the remainder
    $remainder = $sum % 10;
    // Get remainder compliment
    $complement = (string)(10 - $remainder);
    // If complement is 10, set to 0
    if($complement === "10") {
        $complement = "0";
    }
    return ($acn[8] === $complement);
}



参考网站:
http://docs.ultimatemember.com/article/94-apply-custom-validation-to-a-field
https://paulferrett.com/2009/validating-abns-and-acns/

你可能感兴趣的:(Ultimate Member 插件登录页面添加自定义验证功能)