Created
April 11, 2018 16:24
-
-
Save swoboda/d3c042a7d029a017949e388460cd62c7 to your computer and use it in GitHub Desktop.
Custom Number Validation for WooCommerce Flexible Checkout Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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