Comments (6)
Hi angelacode
I need a bit more info to be able to help:
- Is the input just a string?
If yes, you can do e.g. one of these three methods:
"1" << your_string
or
"1" + your_string
or
"1#{your_string}" - Does that input go through phony or does it come from phony and you'd like to add a 1?
Cheers
Florian
from phony.
Hi, there, he's what I do now, whenever I save a contact, I do something like this:
self.phone = "1"+Phony.normalize(self.phone) unless self.phone.empty?
This works fine if I input, say (404)555-1212.
It then saves that as 14045551212.
But what happens when i edit and update that contact's other fields?
It now tries to normalize 114045551212 -- by adding yet another "1"....!
Is there a way to know whether there's a 1 or not or avoid this? Seems like before I can normalize, I need to add the "1".
from phony.
Hi angelacode
Am I correct in assuming you only work with north american numbers?
According to the NANP: http://en.wikipedia.org/wiki/North_American_Numbering_Plan
the digit after the 1 can only be in the range [2-9].
So a cheap solution would be to add a 1 if there isn't one already.
Some sample code you can run:
a = '14045551212'
b = '4045551212'
a = "1#{a}" if a !~ /\A1/
b = "1#{b}" if b !~ /\A1/
p a
p b
We (at my last company) had a more sophisticated solution. We had two fields in the database, a normalized_number and a number. Whenever a user entered a phone number, we saved it in the number field and tried to normalize it. If that succeeded, we saved the normalized number in the normalized_number field.
We used the number from the normalized_number field for purposes like callto: links and displaying a nicely formatted number using Phony.
Did that help you?
Cheers
Florian
from phony.
um..let's see, I have it currently add "1" after normalizing through Phony. But what happens if I update the record? It will still add a "1"...the following, for example, properly normalizes 415-555-1212 to 14155551212, but if I edit, it doesn't...
self.fax = "1"+Phony.normalize(self.fax) unless self.fax.blank? || (self.fax.length == 11 && self.fax[0] == '1')
from phony.
Hi angelacode
Have you maybe got failing specs you can show me?
This issue is starting to move away from Phony, so I'd like to close it. Would that be fine for you?
Concerning your problem:
I do not have all the context of the line you have pasted in the last comment. But…
What I'd do is check if the first number is not a 1, like so:
self.number !~ /\A[^\d]_1/
Then, if yes, add a 1, and normalize the resulting number with Phony, something like this:
self.number = Phony.normalize(add_north_american_country_code_to(self.number)) if self.number !~ /\A[^\d]_1/
I hope that helps, good luck!
Florian
from phony.
I'm closing this one – please reopen if needed.
from phony.
Related Issues (20)
- Phony.plausible? failing for argentina international mobile numbers
- plausible? returns true for invalid USA numbers HOT 1
- Phony.plausible returns false for valid german phone number HOT 3
- Leading 0 incorrectly suppressed from Sammarinese numbers HOT 2
- Incomplete detection of Nigerian phone numbers HOT 1
- Phony.plausible? returns false for a valid Gabon number HOT 1
- Phony.plausible? returns false for a valid Belgian mobile phone number HOT 4
- warning: Capturing the given block using Proc.new is deprecated; use `&block` instead HOT 2
- Phony.normalize('6502001382') returns '652001382' -- '0' was lost HOT 1
- Phony.plausible? returns false for a valid Belgian phone number HOT 2
- Numbering plan of Cote d'Ivoire/Ivory Coast has changed HOT 1
- Handle country codes for countries related to the united states with area code part of the country calling code +1-xxx HOT 3
- Globaltel Serbia phone numbers are not plausible
- Phony plausible? returns false for 3 digit locals with 3 digit NDC (German Numbers) HOT 5
- `false` is being inserted into formatted phone number, when no NDC is being used HOT 3
- Mauritan number definition does split but normalize HOT 3
- Samoa valid number that starts with "71" was not validated HOT 3
- Phony.plausible? returns false for valid Columbian numbers HOT 2
- Valid Italian phone number fails plausibility check HOT 2
- Proposal for "no RAM" mode HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phony.