Answers.Body: <p>There's an extension that does precisely this: <a href="https://github.com/cividesk/com.cividesk.normalize">https://github.com/cividesk/com.cividesk.normalize</a></p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T20:53:15.220Z
Answers.Text: 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.
Answers.UserDisplayName: Joe Murray
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T20:55:34.383Z
Answers.Text: 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.
Answers.UserDisplayName: Andie Hunt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T21:25:57.573Z
Answers.Text: 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. :(
Answers.UserDisplayName: Joe Murray
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-17T21:50:54.627Z
Answers.Text: I agree, not ideal at all.  It could definitely stand improvement.
Answers.UserDisplayName: Andie Hunt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T14:00:22.977Z
Answers.LastActivityDate: 2015-06-16T14:00:22.977Z
Answers.UserDisplayName: Andie Hunt
Answers.Body: <p>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 <code>type="tel"</code>, 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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T13:44:45.420Z
Answers.LastActivityDate: 2015-06-16T13:44:45.420Z
Answers.UserDisplayName: Nicholai
Answers.Body: <p>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 <code>phone_numeric</code> field in the database. This is the field that is used for search.</p>
  
  <p>It doesn't enforce a certain number of digits, but thought you'd want to know that this calculated field exists.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-29T15:43:54.360Z
Answers.Text: Is there a way to use the phone_numeric field in a report?
Answers.UserDisplayName: Dave T
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-29T20:03:58.203Z
Answers.Text: Yes it's available in SearchKit if you make your report that way.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T20:54:19.483Z
Answers.Text: Mmm, might be quite helpful, since we can use it to format the data on presentation if number of digits is 10.
Answers.UserDisplayName: Joe Murray
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T14:32:57.637Z
Answers.LastActivityDate: 2015-06-16T14:32:57.637Z
Answers.UserDisplayName: Coleman
Answers.Body: <p>Rather than trying to solve it at the form level, you could use <code>hook_civicrm_post()</code> to look for a 'Phone' object. From there, doing a phone number reformat by talking to <a href="https://github.com/googlei18n/libphonenumber" rel="nofollow">libphonenumber</a> 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 <a href="https://github.com/giggsey/libphonenumber-for-php" rel="nofollow">PHP port</a> of that library.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T13:29:22.373Z
Answers.LastActivityDate: 2015-06-16T13:29:22.373Z
Answers.UserDisplayName: Nicholai
Body: <p>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?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-17T09:06:29.337Z
Comments.Text: I want to plug https://github.com/JohnFF/UK-Phone-Number-Validator here, as it's written by a friend and former boss and we use it, but it's UK only.
Comments.UserDisplayName: DaveFF
Tags: <extension><validation><phone>
Title: Simple, consistent N. Am. phone validation/reformatting everywhere?
ThreadText: # Simple, consistent N. Am. phone validation/reformatting everywhere?
  
  (Asked by: username_0 on 2015-06-16)
  
  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?
  
  **Tags:** extension, validation, phone
  
  > **Comments:**
  > **username_1**: I want to plug https://github.com/JohnFF/UK-Phone-Number-Validator here, as it's written by a friend and former boss and we use it, but it's UK only.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2015-06-16. Score: 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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_2**: 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.  
  > **username_0**: 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. :(  
  > **username_2**: I agree, not ideal at all.  It could definitely stand improvement.  
  
  ---
  
  ### Answer by username_3 on 2015-06-16. Score: 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.
  
  
  ---
  
  ### Answer by username_4 on 2015-06-16. Score: 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.
  
  > **Comments:**
  > **username_0**: Mmm, might be quite helpful, since we can use it to format the data on presentation if number of digits is 10.  
  > **username_5**: Is there a way to use the phone_numeric field in a report?  
  > **username_4**: Yes it's available in SearchKit if you make your report that way.  
  
  ---
  
  ### Answer by username_3 on 2015-06-16. Score: 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](https://github.com/googlei18n/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](https://github.com/giggsey/libphonenumber-for-php) 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.
  
  
  ---
  
  