Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating email and phone number using java

 
Jim Shapt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to validate email and phone number on the server. It works but trying to validate if the email is entered correctly. Also phone number has the correct number of digits.

Here's what I have so far not sure how to write it

For some reason I can't place the code in here. It says I have a illegal HTML code < I don't have any HTML code. Can somebody give me some advice here?
[ December 14, 2004: Message edited by: Jim Shapt ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've got something that looks like a JavaScript event handler, it'll get rejected. Otherwise just "<" characters are perfectly legal.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The names "on Load" and "get Cookie" (without the spaces) will get your message rejected, for example.
 
Jim Shapt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to validate email and phone number on the server. It works but trying to validate if the email is entered correctly - @ and/or . . Also phone number has the correct number of digits.

Here's what I have so far not sure how to write it



Weird Ok I figured out it wont take val--dation. So now everything is Val or ValError

---------------------------------------------------------------------------



if (PhoneNumber.equals("")) {
val = false;
valerror = valerror + "Phonenumber invalid;";
System.out.println(val);
}

if (EmailAddress.equals("")) {
val = false;
valerror = valerror + "Email address invalid;";
System.out.println(val);

}


if (val == true)


{
return "val";

}
else
{

return valerror;
}


----------------------------------------------------------------------------
[ December 14, 2004: Message edited by: Jim Shapt ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tough going.
Email addresses can be pretty much anything, depending on where they're used.
Generally, all you can say is that it's made up of 2 strings with an at-sign (@) in between but that's not always the case (for example on internal company networks).

Much more than that you can't say...

Phone numbers are even worse. Most will be at least 3 digits long (again on internal networks), and can be far longer (12 or more for international numbers).
 
Jim Shapt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok can somebody give me an example?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One place to start is to decide what kind of phone numbers and email addresses you are going to accept. As mentioned above, there are different variations in some situations. Perhaps the first step in this decision process is identifying who will use this program. Will they be likely to enter just their email user name and expect it to be delivered on the local network? Typically this can be avoided by forcing them to enter a domain name. For phone numbers, are you going to allow international prefixes or just the standard format in your own country?

Consider these questions and try to come up with an informal description of what is (and is NOT) allowed as an email address and as a phone number. Feel free to post here as you are thinking about the issues involved. Hopefully the questions above sparked some thoughts. We'll be glad to help you out from there.

Keep Coding!

Layne
 
Jim Shapt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up above I have what I need for the email. I want to make sure they have an @ symbol and a . period. The phone number needs to be 10 digits long.
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well any time I've tackled this I've always used regualr expressions:

(\w.)+@\w+(.[\w])+
will get you started for the email but you'll have to look up the specifications to gather all valid characters in an address

As with phone numbers if you'll only accept digits you're it's straight forward
(\d){10} but you might want to give some leeway and say between 6 & 10 digits
(\d){6,10}

Hope this helps,
Barry
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd recommend using the javax.mail.internet.InteretAddress class to validate SMTP email addresses since it will do it all for you (there nothing out the box if you want to validate another protocol).

To get you going (and to save you having to read RFC822) I'll can extend on what Jeroen has already told you: SMTP email addresses do not need to include an '@' sign. They can contain white space. And can be "unfolded" - which means they span more then one line. They can include commas and angle brackets (i.e. <> ) so long as they are in the correct place.
[ December 15, 2004: Message edited by: Paul Sturrock ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Shapt:
Up above I have what I need for the email. I want to make sure they have an @ symbol and a . period. The phone number needs to be 10 digits long.

If this is all you want to check for, then you can just use some methods in the String class. In particular, indexOf() and lastIndexOf() might be particularly useful.

I agree that regular expressions are also a powerful way to solve this problem. However, imho, the above is much simpler.

HTH

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic