| Author |
Validating email and phone number using java
|
Jim Shapt
Greenhorn
Joined: Dec 13, 2004
Posts: 10
|
|
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
Joined: Jul 08, 2003
Posts: 24054
|
|
|
If you've got something that looks like a JavaScript event handler, it'll get rejected. Otherwise just "<" characters are perfectly legal.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
|
The names "on Load" and "get Cookie" (without the spaces) will get your message rejected, for example.
|
 |
Jim Shapt
Greenhorn
Joined: Dec 13, 2004
Posts: 10
|
|
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
Joined: Oct 12, 2000
Posts: 5093
|
|
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).
|
42
|
 |
Jim Shapt
Greenhorn
Joined: Dec 13, 2004
Posts: 10
|
|
|
Ok can somebody give me an example?
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
Jim Shapt
Greenhorn
Joined: Dec 13, 2004
Posts: 10
|
|
|
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
Joined: Jun 05, 2003
Posts: 89
|
|
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
Joined: Apr 14, 2004
Posts: 10336
|
|
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 ]
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
 |
 |
|
|
subject: Validating email and phone number using java
|
|
|