Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lucas Ward   on May 19 In Java Category.

  
Question Answered By: Wilfred Young   on May 19

validation of phone  numbers strongly depends on the country you're
working in; in USA you have very easy phone numbers, they all follow
the format aaa.ppp.nnnn where aaa = area code, ppp = prefix, and nnnn
= number within this prefix. However, in different countries you have
utterly different systems; the USA is the only country that I know
where the area code always has the same length; in Germany you have
area codes between 3 and 6 digits, in Italy you sometimes have to
dial a 0 as the first digit of area code and sometimes not, and the
like... In order to parse phone numbers  for validity you first should
know very well for what countries you're doing this.

Second problem is the use of delimiting characters. I've seen US
phone numbers in different formats, such as 262-555-1234 or
262.555.1234 or even 262/555 1234 and some more. So maybe you should
first try to find out whether you can rely on a particular format to
parse.

On to the actual process of parsing.
Every Java string  has a fixed length (as long as you look at the
string and don't change it, of course). So you could set up a loop
over all the characters in one string:
for (int currentIndex = 0; currentIndex <
myString .length(); currentIndex++)
if (...)
{ ... }

I assume you want to know what condition to write into the if()
clause? Well, as I wrote above I suggest you first find out what
format of numbers you may scan; then we can see how to do it.

About email  addresses: that's easier. There are official documents as
to what characters may comprise a valid email address; these
documents can be obtained for example from http://www.rfc.org , but
in general you can assume that an email address always looks like
this:
some.one@...
The part "some.one" must start with a letter, then there may be an
arbitrary number of letters, digits, underscores, dots '.', and
dashes.
The part "some.where.tld" (tld = Top Level Domain, these are managed
worldwide by the NIC, try http://www.nic.org (?)) is built in the
same way.
This means that in your loop above you have to check that:
- each part begins with a letter,
- that there is exactly one at character '@' and that this is neither
at the beginning nor at the end of the string,
- that after the initial letter in both parts only allowed characters
follow.
If you feel confident enough with Java, you can try to check that the
TLD part only has one of the allowed values, but that's a bit
dangerous because they may be expanded at any time.

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How do I validate Phone numbers in java/ email addresses Or get search suggestion and latest updates.


Tagged: