• Resolved yousj0226

    (@yousj0226)


    Hi, i would like to add custome Japanese ‘Katakana’ validation.
    This is my code below,
    but it isn’t work.
    do you have any idea to make it work?

    thank you

    function wpdesk_fcf_validate_katakana( $field_label, $value ) {
    if ( filter_var( $value,’/^[ァ-ヾ]+$/u ‘ ) == false ) {
    wc_add_notice( sprintf( ‘%s is not a valid katakana.’, ‘‘ . $field_label . ‘‘ ), ‘error’ );
    }
    }

    add_filter( ‘flexible_checkout_fields_custom_validation’, ‘wpdesk_fcf_custom_validation_katakana’ );
    /**
    * Add custom URL validation
    *
    */
    function wpdesk_fcf_custom_validation_url( $custom_validation ) {
    $custom_validation[‘katakana’] = array(
    ‘label’ => ‘katakana’,
    ‘callback’ => ‘wpdesk_fcf_validate_katakana’
    );

    return $custom_validation;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Tomasz WP Desk

    (@tomaszwp)

    Hello @yousj0226

    Please use this general code but adapt it to this particular case.
    if ( preg_match( ‘/^(\p{Katakana})+/’, $value ) ) {

    class KatakanaValidator {
    
        // unique validation key to avoid validation duplication
        const VALIDATION_KEY   = 'Katakana Validation';
        
        // change 'text-domain' string to custom text domain to allow translating the field. 
        const THEME_TEXTDOMAIN = 'text-domain';
    
        public function hooks() {
            add_filter( 'flexible_checkout_fields_custom_validation', [ $this, 'add_custom_validation_rule' ] );
        }
    
        public function add_custom_validation_rule( array $custom_validations ): array {
            $custom_validations[ self::VALIDATION_KEY ] = [
                'label'     => 'Katakana validation',
                'callback'  => [ $this, 'validate_field_value' ],
            ];
    
            return $custom_validations;
        }
    
        /**
         * @return void
         */
        public function validate_field_value( $field_label, $value ) {
            if ( preg_match( '/^(\p{Katakana})+/', $value ) ) {
                return;
            }
    
            wc_add_notice(
                sprintf(
                    __( '%s is not a valid katakana string.', self::THEME_TEXTDOMAIN ),
                    '<strong>' . $field_label . '</strong>'
                ),
                'error'
            );
        }
    }
    
    ( new KatakanaValidator() )->hooks();
    Thread Starter yousj0226

    (@yousj0226)

    @tomaszwp

    It works perfectly for me!!
    Thank you so much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Japanese Katakana validation code’ is closed to new replies.