Walidacja NIP-u i kodu pocztowego w Contact Form 7

Strona główna / Blog / Wordpress / Walidacja NIP-u i kodu pocztowego w Contact Form 7

W poniższym przykładzie pokazuję jak poprawnie zwalidować polski NIP i kod pocztowy, kiedy używamy popularnej wtyczki Contact Form 7 w WordPressie. Post powstał na potrzeby chwili… 🙂

Dla kodu pocztowego, w functions.php dodajemy

// Add custom validation for CF7 form fields
function is_company_code($text){ // Check against list of common public email providers & return true if the email provided doesn't match one of them
if(
preg_match('/(\b\d{2}\s*-\d{3}\b)/i', $text)
){
return true; // It's a publicly available email address
}else{
return false; // It's probably a company email address
}
}

add_filter('wpcf7_validate_text', 'your_validation_filter_func', 999, 2);
 add_filter('wpcf7_validate_text*', 'your_validation_filter_func', 999, 2);
 function your_validation_filter_func($result, $tag) {
    $type = $tag['type'];
    $name = $tag['name'];

   if($name == 'kod_pocztowy'){ // Only apply to fields with the form field name of "comany-email"
   $the_value = $_POST[$name];
   if(!is_company_code($the_value)){ // Isn't a company email address (it matched the list of free email providers)
   $result['valid'] = false;
   $result->invalidate($tag, wpcf7_get_message('invalid_coupon_code'));
   }
   }
  return $result;
}



add_filter('wpcf7_messages', 'customwpcf7_text_messages');

function customwpcf7_text_messages($messages) {
    return array_merge($messages, array(
'invalid_coupon_code' => array(
     'default' => __('Kod jest niewłaściwy', 'contact-form-7')
  )

));
}

, gdzie 'kod_pocztowy’ to nazwa walidowanego pola w CF7.

Dla NIP-u metdyka jest dość symilarna:

// Add custom validation for CF7 form fields
function is_company_nip($text){ // Check against list of common public email providers & return true if the email provided doesn't match one of them
$valid = false;
    $weights = array( 6, 5, 7, 2, 3, 4, 5, 6, 7 );
    $nip = preg_replace( '/^PL/', '', $text );
    if( strlen( $nip ) == 10 && is_numeric( $nip ) ) {
        $sum = 0;
        for( $i = 0; $i < 9; $i++ )
            $sum += $nip[$i] * $weights[$i];
        $valid = ( $sum % 11 ) == $nip[9];
        return true; // It's a publicly available email address
    }

else {
return false; // It's probably a company email address
}
}

add_filter('wpcf7_validate_text', 'validation_nip', 999, 2);
add_filter('wpcf7_validate_text*', 'validation_nip', 999, 2);
 function validation_nip($result, $tag) {
    $type = $tag['type'];
    $name = $tag['name'];

   if($name == 'nip'){ // Only apply to fields with the form field name of "comany-email"
   $the_value = $_POST[$name];
   if(!is_company_nip($the_value)){ // Isn't a company email address (it matched the list of free email providers)
   $result['valid'] = false;
   $result->invalidate($tag, wpcf7_get_message('invalid_nip'));
   }
   }
  return $result;
}



add_filter('wpcf7_messages', 'customnip_text_messages');

function customnip_text_messages($messages) {
    return array_merge($messages, array(
'invalid_nip' => array(
     'default' => __('Pole jest wypełnione niepoprawnie. Wpisz polski numer NIP bez spacji i myślników.', 'contact-form-7')
  )

));
}

Tutaj analogicznie 'nip’ jest nazwą pola w CF7.