• Resolved sudhirsinghal

    (@sudhirsinghal)


    Dear Support Team,

    I am using the forminator form to get phone number input from the users.
    In the Validation of the field, I selected International to include the country code. I am transferring the data to my Google Sheet. The number is stored in the format '+91 91234 56789' in the sheet. How can we trim the spaces in between and remove any special characters, including the plus '+' sign. Actually, I need to send an automated message to the users for confirmation, and API needs the country code, the phone number, and no other characters.
    Your team shared the following code:
    
    <?PHP 
    /** * Plugin Name: Forminator Custom Phone Formatting +91 XXXXX XXXXX. 
    * Description: Forminator Custom Phone Formatting +91 XXXXX XXXXX. */ // Add the custom script to the footer 
    add_action('wp_footer', function () { 
    ?> 
    <script> 
    (function ($) { 
    $(function () { 
    $(document).on("after.load.forminator", function (e, id) { 
    // Phone number formatting logic 
    $('#forminator-field-phone-1').on('input', function () { 
    var number = $(this).val().replace(/[^\d]/g, ''); // Remove non-digit characters 
    // Remove all white spaces 
    number = number.replace(/\s+/g, ''); if (number.length === 10) { 
    // Format as "XXXXXXXXXX" 
    number = number.replace(/(\d{10})/, '$1'); 
    } 
    // Set the formatted number as the input value 
    $(this).val(number); 
    }); 
    }); 
    }); 
    })(window.jQuery); 
    </script> 
    <?PHP 
    }, 21);
    
    I entered the above code in a PHP file in mu-plugins folder.
    But on submission of the  form it is still storing the number in the same old format with + sign and spaces.
    I have checked the file permissions and saved the php file as per the in the following link: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
    
    Kindly guide what code or settings I need to update.
    
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @sudhirsinghal,

    I hope you are keeping well and thank you for reaching out to us.

    Can you please try the following slightly updated code and check if that helps?

    <?php
    add_action('wp_footer', function () { 
    ?> 
    <script> 
    (function ($) { 
    $(function () { 
    $(document).on("after.load.forminator", function (e, id) { 
        
    var $uid = $(e.target).attr('data-uid');
    // Phone number formatting logic 
    $('#forminator-field-phone-1' + $uid).on('input', function () { 
    var number = $(this).val().replace(/[^\d]/g, ''); // Remove non-digit characters 
    // Remove all white spaces 
    number = number.replace(/\s+/g, ''); if (number.length === 10) { 
    // Format as "XXXXXXXXXX" 
    number = number.replace(/(\d{10})/, '$1'); 
    } 
    // Set the formatted number as the input value 
    $(this).val(number); 
    }); 
    }); 
    }); 
    })(window.jQuery); 
    </script> 
    <?php 
    }, 21);

    I hope that helps. Please feel free to get back to us if you need any further assistance.

    Kind Regards,
    Nebu John

    Thread Starter sudhirsinghal

    (@sudhirsinghal)

    Thanks for your reoly.

    The updated code is not working. It still stores the number in the previous format with country code and spaces.
    I check the page’s source code to ensure the script is getting loaded.

    I tried lowering the script pirority to 5 from 21 but no change in the result.
    Kindly suggest what else need to do to solve the issue.

    Best Regards,
    Sudhir Singhal

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @sudhirsinghal,

    I hope you are doing well today!

    You can try the following code snippet as mu-plugin instead.

    <?php
    add_action( 'forminator_custom_form_submit_before_set_fields', 'phone_number_correction', 10, 3 );
        function phone_number_correction( $entry, $module_id, $field_data_array ) {
        $form_ids = array(3730); // Please change the form ID.
        if ( !in_array( $module_id, $form_ids ) ) {
            return;
        }
    
        foreach ( $field_data_array as $key => $value ) {
            if ( 'phone-1' == $value['name'] ) {
                $new_phone = str_replace( '+2120', '+212', $value['value'] );
                $new_phone = str_replace(' ', '', $new_phone);
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($new_phone);
            }
        }
    }
    
    add_filter( 'forminator_prepared_data', 'phone_number_correction_prepared', 10, 2 );
    function phone_number_correction_prepared( $prepared_data, $module_object ){
    	if ( $module_object->id != 3730 ) { // Please change the form ID.
    		return $prepared_data;
    	}
    
    	if ( ! empty( $prepared_data['phone-1'] ) ) {
            $new_phone = str_replace( '+2120', '+212', $prepared_data['phone-1'] );
            $new_phone = str_replace(' ', '', $new_phone);
    		$prepared_data['phone-1'] = sanitize_text_field($new_phone);
    	}
    
    	return $prepared_data;
    }

    Please make sure to update the code with your form ID and

    $new_phone = str_replace( '+2120', '+212', $prepared_data['phone-1'] );
    

    Above, the first one?+2120?is the country code and?+212?is the replacement, so you can use only?''?instead of using +212

    Kind regards,
    Zafer

    Thread Starter sudhirsinghal

    (@sudhirsinghal)

    Thanks for the updated script.
    It works now; I replaced the country code ‘+2120’ with ‘+’ to trim the + sign from all the numbers, irrespective of any country the user selects.
    I don’t understand the use of the add_filter function here. I think the first function, ‘add_action,’ is doing the required task. Is there any link to learn more about it?

    Thanks again for your support.

    Best Regards,
    Sudhir Singhal

    Thread Starter sudhirsinghal

    (@sudhirsinghal)

    How do we apply the above script to the other forms?
    If I want to use the same script for another form, do I need to create a new PHP file with the other form ID, or can I add two or more form IDs in the same script?

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @sudhirsinghal,

    You can find below the both answers;

    I don’t understand the use of the add_filter function here. I think the first function, ‘add_action,’ is doing the required task. Is there any link to learn more about it?

    The add_filter function is used to modify data by applying a ‘filter’ to it. In this case, the second function uses add_filter to manipulate the prepared data before it is finally processed.

    After the form is submitted, but before the entry is saved in the database, the ‘phone-1’ field is checked. If it is not empty, it removes the extra ‘0’ from ‘+2120’ and makes it ‘+212’, and also removes any spaces from the phone number. After this, sanitize_text_field is used to sanitize the phone number (to ensure it only contains valid and safe data) and then it returns the prepared data.

    You can think of?add_filter?as a way to ‘intercept’ data and modify it. The reason both?add_action?and?add_filter?are used here is to ensure the modification occur at different stages of the process: initially when setting the fields (add_action) and later when preparing the final data for processing (add_filter).

    How do we apply the above script to the other forms?
    If I want to use the same script for another form, do I need to create a new PHP file with the other form ID, or can I add two or more form IDs in the same script?

    You can add two or more form IDs in the same script such as;

    $form_ids = array(3730,3732,3735); // Add as many IDs as necessary.

    and for if statement;

    $valid_ids = array(3730, 3732, 3735); // Add as many IDs as necessary.
    if ( !in_array( $module_object->id, $valid_ids ) ) {
        return $prepared_data;
    }
    

    Kind regards,
    Zafer

    Thread Starter sudhirsinghal

    (@sudhirsinghal)

    Thank you so much for the explanation and the solution you gave, it saves my lot of time.

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    You’re welcome, @sudhirsinghal

    We’re really glad that the PHP snippet helped. I’ll be marking the thread as resolved.

    Please feel free to re-open it, in case you still have any questions!

    Best Regards,
    Dmytro

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Phone number formatting’ is closed to new replies.