Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. defmodule Rocket.Stringer do
  2.  
  3. def sanitize_phone(ph) do
  4. ph = Regex.replace(~r/[^0-9]/,ph,"")
  5. case Regex.run(~r/\d{5,10}$/,ph) do
  6. [phone] -> phone
  7. _ -> nil
  8. end
  9. end
  10.  
  11.  
  12. def format_phone(ph) do
  13. ph = sanitize_phone(ph)
  14. case String.length(ph) do
  15. 10 -> "(#{String.slice(ph,0,3)})#{String.slice(ph,3,3)}-#{String.slice(ph,6,4)}"
  16. 7 -> "#{String.slice(ph,0,3)}-#{String.slice(ph,3,4)}"
  17. _ -> ph
  18. end
  19. end
  20.  
  21. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement