Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Rocket.Stringer do
- def sanitize_phone(ph) do
- ph = Regex.replace(~r/[^0-9]/,ph,"")
- case Regex.run(~r/\d{5,10}$/,ph) do
- [phone] -> phone
- _ -> nil
- end
- end
- def format_phone(ph) do
- ph = sanitize_phone(ph)
- case String.length(ph) do
- 10 -> "(#{String.slice(ph,0,3)})#{String.slice(ph,3,3)}-#{String.slice(ph,6,4)}"
- 7 -> "#{String.slice(ph,0,3)}-#{String.slice(ph,3,4)}"
- _ -> ph
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement