Skip to content

Instantly share code, notes, and snippets.

@swoboda
Created April 11, 2018 16:24
Show Gist options
  • Save swoboda/d3c042a7d029a017949e388460cd62c7 to your computer and use it in GitHub Desktop.
Save swoboda/d3c042a7d029a017949e388460cd62c7 to your computer and use it in GitHub Desktop.
Custom Number Validation for WooCommerce Flexible Checkout Fields
<?php
// Do NOT include the opening php tag
/**
* Function to validate the number
*
*/
function wpdesk_fcf_validate_number( $field_label, $value ) {
if ( ! ( ( is_numeric( $value ) ? intval( $value ) == $value : false ) ) ) {
wc_add_notice( sprintf( '%s is not a valid number.', '<strong>' . $field_label . '</strong>' ), 'error' );
}
}
add_filter( 'flexible_checkout_fields_custom_validation', 'wpdesk_fcf_custom_validation_number' );
/**
* Add custom number validation
*
*/
function wpdesk_fcf_custom_validation_number( $custom_validation ) {
$custom_validation['number'] = array(
'label' => 'Number',
'callback' => 'wpdesk_fcf_validate_number'
);
return $custom_validation;
}
@nextlevelweb
Copy link

Dear Swoboda,

Do you have any suggestions to combine number validation with letters validation? in my country we use letters and numbers for bank accounts. I'm not that good at programming yet.

Kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment