使用 11 检查荷兰 BSN 验证文本字段

Validate textfield with 11 check for Dutch BSN

我在 Wordpress 网站上使用 CF7,如果文本字段包含 9 位数字并使用所谓的 11-check,我想验证它。这是一种验证荷兰 SSN (BSN) 的算法。

它是这样工作的:

假设我们对 BSN 123456782 执行 11-check

1st digit = 1, 9 * 1 = 9
2nd digit = 2, 8 * 2 = 16
3rd digit = 3, 7 * 3 = 21
4th digit = 4, 6 * 4 = 24
5th digit = 5, 5 * 5 = 25
6th digit = 6, 4 * 6 = 24
7th digit = 7, 3 * 7 = 21
8th digit = 8, 2 * 8 = 16
9th digit = 2, -1 * 2 = -2 (last digit is not added but subtracted)
 
Total is 154
 

因为 154 可以除以 11,而且它是一个 9 位数字,我们可以假设它是一个有效的 BSN(154/11=14)。

我认为这可以用 <script></script> 以及一些 jQuery 来完成。 我还在 Github 上找到了这个(部分)代码,我认为它很有用,但我对此很陌生,所以希望有人能帮助我。

function addLeadingZerosToBSN(bsn) {
    return ("000000000" + bsn).slice(-9);
}

function getSumBSN(bsn) {

  var a = parseInt(bsn[0])*9;
  var b = parseInt(bsn[1])*8;
  var c = parseInt(bsn[2])*7;
  var d = parseInt(bsn[3])*6;
  var e = parseInt(bsn[4])*5;
  var f = parseInt(bsn[5])*4;
  var g = parseInt(bsn[6])*3;
  var h = parseInt(bsn[7])*2;
  var i = parseInt(bsn[8])*-1;

  var sum = a+b+c+d+e+f+g+h+i;
  return sum;
}

function isValidBSN(bsn) {
  bsn = addLeadingZerosToBSN(bsn);

 
  if (getSumBSN(bsn) % 11) {
    return false;
  } else {
    return true;
  }
}

找到此代码,但它不适用于 CF7:

function check_bs(){

var fieldRequired = Array("extra_1", "extra_2", "extra_3", "extra_4", "extra_5");
var alertMsg = "De volgende BS-nummers zijn niet correct:\n";
var l_Msg = alertMsg.length;

// loopje van de ingevulde velden
for (var i = 0; i < fieldRequired.length; i++){

bsnr=formobj.elements[fieldRequired[i]];
checksum=0;

// check cijfers
if(isNaN(bsnr) || bsnr.length!=9){
alertMsg += i + ". (te kort)" + "\n"
}
// check elfproef 
else{
for(i=0;i<8;i++){
checksum += (bsnr.charAt(i)*(9-i));
}
checksum -= bsnr.charAt(8);

// ongeldig nummer
if(checksum%11!=0){
alertMsg += i + ". (ongeldig)" + "\n"
}
}

}

if (alertMsg.length == l_Msg){
return true;
} else {
alert(alertMsg);
return false;
}
}

提前致谢。

瓦斯科

我已经设法回答了我自己的问题。希望这对将来的某人有帮助。它适用于所有使用 'eleven check'.

的东西

我的代码:

add_filter( 'wpcf7_validate_text*', 'bsn_field_validation', 20, 2 );
function bsn_field_validation($result, $tag ) {

$fields_to_validate = [
    'field-name'
];

if ( in_array($tag->name, $fields_to_validate) ) {

$bsn_number = isset( $_POST[$tag->name] ) ? trim( $_POST[$tag->name] ) : '';

    if (strlen($bsn_number) != 9 ) {
        $result->invalidate( $tag, "Field not valid!" );
        return $result;
    }

    $number = str_split($bsn_number);

    $bsn_counter = 0;
    $sum = 0;
    for ( $i = 9; $i >= 1 ; $i--) {
        if ($i == 1) {
            $sum = $sum - ($number[$bsn_counter] * $i);
        } else {
            $sum = $sum + ($number[$bsn_counter] * $i);
        }
        
        $bsn_counter++;
    }

    if ($sum % 11 != 0) {
        $result->invalidate( $tag, "Field not valid!" );
        return $result;
    }
    return $result;
  }
  return $result;
  }

add_action( 'wp_footer', 'validate_cf7_bsn_field' );
function validate_cf7_bsn_field() {
?>
<script>

jQuery(document).ready(function($) {

    const fieldsToValidate = [
        'field-name'
    ];

    fieldsToValidate.forEach(function(v, i) {
        $('input[name="' + v + '"]').on('blur', function() {
            const bsnNumber = $(this).val().trim();
            $(this).removeClass('wpcf7-not-valid');
            $(this).parent().find('.wpcf7-not-valid-tip').remove();
            if (bsnNumber.length != 9) {
                $(this).addClass('wpcf7-not-valid');
                $(this).parent().append('<span class="wpcf7-not-valid-tip" aria-hidden="true">Field not valid!</span>');
                return false;
            } else if (Number(bsnNumber) == NaN) {
                $(this).addClass('wpcf7-not-valid');
                $(this).parent().append('<span class="wpcf7-not-valid-tip" aria-hidden="true">Field not valid!</span>');
                return false;
            }

            bsnCounter = 0;
            sum = 0;
            for ( i = 9; i >= 1 ; i--) {
                if (i == 1) {
                    sum = sum - (bsnNumber[bsnCounter] * i);
                } else {
                    sum = sum + (bsnNumber[bsnCounter] * i);
                }
                
                bsnCounter++;
            }

            if (sum % 11 != 0) {
                $(this).addClass('wpcf7-not-valid');
                $(this).parent().append('<span class="wpcf7-not-valid-tip" aria-hidden="true">Field not valid!</span>');
            } else {
                $(this).removeClass('wpcf7-not-valid');
                $(this).parent().find('.wpcf7-not-valid-tip').remove();
            }
        });
    });
    
});
</script>

<?php
}

如果您想添加更多字段以在同一个(或网站上的其他表单)中进行验证,您只需在 $fields_to_validate = [ 下面的第一个字段下方和 [=15] 下面的第一个字段下方添加额外的字段名称=].

假设您要验证 3 个字段。它现在说:

$fields_to_validate = [
    'field-name'

改为:

$fields_to_validate = [
    'field-name'
    'second-field-name',
    'third-field-name',

同样对于 const fieldsToValidate = [,您需要以相同的方式添加这些字段名称:

const fieldsToValidate = [
    'field-name',
    'second-field-name',
    'third-field-name'