7

If we want to put in some validation and reformatting for phone numbers to make sure they conform to N. Am. standard for 10 digit numbers, is there a simple way to catch this for everywhere a phone number is entered or edited (including edit in place), rather than having to search and deal with each form separately?

1

4 Answers 4

7

There's an extension that does precisely this: https://github.com/cividesk/com.cividesk.normalize

It used to be in the extensions directory, but it's gone now for some reason. It uses libphonenumber, and it formats international numbers appropriately based upon the country in the address.

4
  • skimming that extension's code it appears it does batch normalization of the phone numbers, but does NOT do it on data entry with feedback to user. FWIW, my guess is that it's not in the extensions directory since Nicolas does not want to have to provide support to users for it.
    – Joe Murray
    Commented Jun 16, 2015 at 20:53
  • It'll do it as you enter contacts one-by-one, but only after submission, so there's no visual feedback of what it'll become.
    – Andie Hunt
    Commented Jun 16, 2015 at 20:55
  • So if there is an input error of 9 or 11 digits, or 10 digits starting with 1 or 0, the garbage will get into the system. :(
    – Joe Murray
    Commented Jun 17, 2015 at 21:25
  • I agree, not ideal at all. It could definitely stand improvement.
    – Andie Hunt
    Commented Jun 17, 2015 at 21:50
4

I'm not sure where exactly phone input fields are built (is it always Phone.php?). But if building phone inputs can be patched to add type="tel", you could then put JS in the footer of all Civi pages to scan for that input type and run your required logic against it.

4

FYI Civi already does one pass at normalization behind the scenes - whenever a phone number is entered or updated, a numeric-only version of it (stripped of all punctuation and spaces) is stored in the phone_numeric field in the database. This is the field that is used for search.

It doesn't enforce a certain number of digits, but thought you'd want to know that this calculated field exists.

3
  • Mmm, might be quite helpful, since we can use it to format the data on presentation if number of digits is 10.
    – Joe Murray
    Commented Jun 16, 2015 at 20:54
  • Is there a way to use the phone_numeric field in a report?
    – Dave T
    Commented Apr 29, 2022 at 15:43
  • Yes it's available in SearchKit if you make your report that way.
    – Coleman
    Commented Apr 29, 2022 at 20:03
3

Rather than trying to solve it at the form level, you could use hook_civicrm_post() to look for a 'Phone' object. From there, doing a phone number reformat by talking to libphonenumber is pretty straightforward. I've built a little Node app that exposes it as a service; if that helps I could chuck it onto GitHub. There is also a PHP port of that library.

I realize it's not ideal as we are cleaning up after the data comes in rather than bringing it in pristine, but it's a smaller dev effort for something workable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.