Support

Account

Home Forums ACF PRO How to format a phone number

Solving

How to format a phone number

  • We are using an open text field to store a phone number, we store it as +1234567890.

    Q1. Is there a way to restrict the text field to just numbers? I’ve tried using the Number field but it works as a counter with arrows up and down.

    Q2. Is there a way to display the number on the front end like +123 (4) 567 890, i.e. with spaces brackets etc. I’d still like it linked (tel:+1234567890).

    Any help greatly appreciated.

  • To limit input on a text field you would use an acf/validate_value filter.

    For formatting the number you need to do that yourself. I would try a search for formatting phone numbers in PHP, here is one example result https://stackoverflow.com/questions/4708248/formatting-phone-numbers-in-php.

    You could potentially use and acf/format_value filter, but this would probably be overkill unless you are using displaying the field in multiple templates, on the other hand if you are using a page builder that is capable of using ACF fields then this filter will probably be needed.

  • I’m not much of a coder s will probably just leave it. Thanks all the same.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.