Created
April 11, 2018 16:31
-
-
Save swoboda/aeec51840f33d82cfe9785e145f10a66 to your computer and use it in GitHub Desktop.
Custom Min/Max Character Limit 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 length | |
* | |
*/ | |
function wpdesk_fcf_validate_length( $field_label, $value ) { | |
$min_length = 3; | |
$max_length = 20; | |
$value_length = strlen( $value ); | |
if ( $value_length < $min_length || $value_length > $max_length ) { | |
wc_add_notice( sprintf( '%s is not valid. The length must be between %s and %s characters.', '<strong>' . $field_label . '</strong>', $min_length, $max_length ), 'error' ); | |
} | |
} | |
add_filter( 'flexible_checkout_fields_custom_validation', 'wpdesk_fcf_custom_validation_length' ); | |
/** | |
* Add custom length validation | |
* | |
*/ | |
function wpdesk_fcf_custom_validation_length( $custom_validation ) { | |
$custom_validation['length'] = array( | |
'label' => 'Length', | |
'callback' => 'wpdesk_fcf_validate_length' | |
); | |
return $custom_validation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment