Skip Navigation

[Resolved] Phone Number Format (999) 999-9999 or 999-999-9999

This thread is resolved. Here is a description of the problem and solution.

Problem: Format custom Phone field with Number Format (999) 999-9999 or 999-999-9999

Solution: The number field will not allow to save the dash - or brackets () as it's a numeric field.

If you want to save the - or () with your number field, you need to change your phone/fax field type to text (single text field) and then we can use the Toolset form's hook: cred_before_save_data => https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

You can find the proposed solution in this case with the following reply: => https://toolset.com/forums/topic/phone-number-format-999-999-9999-or-999-999-9999/#post-1975289

Relevant Documentation:

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 8 replies, has 2 voices.

Last updated by andrewD-10 8 months, 3 weeks ago.

Assigned support staff: Minesh.

Author
Posts
#1967505

Video: hidden link

Hi Toolset, as described in the video above, I am trying to get the phone numbers to be in a certain format.

Please let me know how I can do this.

#1967859

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The number field will not allow to save the dash - or brackets () as it's a numeric field.

If you want to save the - or () with your number field, you need to change your phone/fax field type to text (single text field) and then we can use the Toolset form's hook: cred_before_save_data
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

And we with the above hook will will adjust the values submitted for your phone/fax as per your desired format.

I hope this makes sense.

#1968377

Hi Minesh,

Ok I see, I went ahead and changed the custom fields to be single line text.

I am not good with coding at all; would you have an example of the code I should use with the 2 custom fields I have. If the example also can be formatted to be an Array so that I can add more than 1 form for this to apply to if it is PHP coding.

Thank you for your help!

#1970123

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL where you added your form as well as admin access details.

In addition to that please share one example how exactly you want to store your phone number.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1974883

How I would like the phone number to be stored is like this with the dashes between first 3 numbers, next 3 numbers, and the final 4 after.

EX: 888-777-1122

Thank you

#1975289

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please check now.

I've added the following hook "cred_before_save_data" to "Custom Code" section with code snippet "Toolset custom Code":
=> hidden link

add_action('cred_before_save_data', 'func_adjust_phone_fax_values',10,1);
function func_adjust_phone_fax_values($form_data){
    // if a specific form
    if ($form_data['id']==69241) {
        if (isset($_POST['wpcf-phone-number']) and !empty($_POST['wpcf-phone-number'])){
          $parts = str_split($_POST['wpcf-phone-number'], 3);
 		  $formatted_number = $parts[0] .'-'. $parts[1] .'-'. $parts[2].$parts[3];
            $_POST['wpcf-phone-number'] = $formatted_number;
           
        }
    }
}

Now, once you save the form the number will be saved in your desired format.

More info:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

#1979107

Hi Minesh,

I tested the code and it works perfectly!

I just need 2 things changed:

1) can you make the code in an array so I can put more than 2 forms to apply this code on?

2) can you also make this code work for both the phone number and fax?

Thank you,
Andrew

#1979279

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

add_action('cred_before_save_data', 'func_adjust_phone_fax_values',10,1);
function func_adjust_phone_fax_values($form_data){

$form_ids = array(69241,9999,8888);

    // if a specific form
    if (in_array($form_data['id',$form_ids)) {

        if (isset($_POST['wpcf-phone-number']) and !empty($_POST['wpcf-phone-number'])){
          $parts = str_split($_POST['wpcf-phone-number'], 3);
          $formatted_number = $parts[0] .'-'. $parts[1] .'-'. $parts[2].$parts[3];
            $_POST['wpcf-phone-number'] = $formatted_number;
            
        }
     if (isset($_POST['wpcf-fax-number']) and !empty($_POST['wpcf-fax-number'])){
          $parts = str_split($_POST['wpcf-fax-number'], 3);
          $formatted_number = $parts[0] .'-'. $parts[1] .'-'. $parts[2].$parts[3];
            $_POST['wpcf-fax-number'] = $formatted_number;
            
        }

    }
}

Please check the above code, where you can pass the multiple: $form_ids

And I've also added the code for fax number, you can adjust the name of your fax number filed if required.

#1980319

Hi Minesh,

The code worked well but I have 1 error on line 16. So I looked at some other examples I had on the array format. I made some changes and the following code works well:

add_action('cred_before_save_data', 'func_adjust_phone_fax_values',10,1);
function func_adjust_phone_fax_values($form_data){

$form_ids = array(83341,83366); // add more form IDs here
if ( in_array($form_data['id'], $form_ids)) { // Edit as required

if (isset($_POST['wpcf-user-contact-phone']) and !empty($_POST['wpcf-user-contact-phone'])){
$parts = str_split($_POST['wpcf-user-contact-phone'], 3);
$formatted_number = $parts[0] .'-'. $parts[1] .'-'. $parts[2].$parts[3];
$_POST['wpcf-user-contact-phone'] = $formatted_number;

}
if (isset($_POST['wpcf-user-contact-fax']) and !empty($_POST['wpcf-user-contact-fax'])){
$parts = str_split($_POST['wpcf-user-contact-fax'], 3);
$formatted_number = $parts[0] .'-'. $parts[1] .'-'. $parts[2].$parts[3];
$_POST['wpcf-user-contact-fax'] = $formatted_number;

}

}
}

Thank you Minesh for you great help!