• Post Reply Bookmark Topic Watch Topic
  • New Topic

Email-Id validation code  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem as follows :
Planet one, a leading data management group has been given the task
of storing the records of their one lakh employees. The record contains an email id whose
datatype is String. The logic for validation of the email id needs to be written and hence
implement the following class EmailCheck:
Fields :
String emailId
Methods:
+setEmailId(String):void
+getEmailId():String
+main(String args[]):void static

The validation rules for the email id check are as follows (to be implemented in main()):
1) Overall Length of the email ID must be greater than 3 and less 20
2) The email ID must include “@” followed by a minimum of 1 and maximum of 2 “.”
3) The substring before “@”must contain a combination of Upper case, Lower case and “_”
symbols.
4) The first letter of the Email ID must be in Upper Case
If all validations are valid,
Display “Email Id is VALID!”
Else,
Display “<Appropriate Error Message>”
 “Invalid Length of email Id”
 “Invalid Position of Special Characters”
 “Invalid Combination for username”
 “Invalid Case of First Letter”


I could able to fulfill only two conditions in validateEmailId() method out of four conditions, and they are :
1) Overall Length of the email ID must be greater than 3 and less 20
2) The email ID must include “@” followed by a minimum of 1 and maximum of 2 “.”
But the other two conditions are hard to implement for me as I didn't find any method which directly checks the usage of UpperCase, LowerCase letters as well as use of '_' before the sign '@'
and method for checking the usage of UpperCase letters for first letter of whole string.

The code written by me is as follows :




Please help me in writing code for other two conditions too.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:Please help me in writing code for other two conditions too.

Well, I'm afraid nobody is going to simply supply you with code (and if they do, it will be removed and the poster reprimanded).

What we are very happy to do though is to help YOU come up with the solution.

And to that end, I have a few suggestions; the first one being:
Don't write a line of Java code until you KNOW what you want to do.

You may find the StopCoding (←click) page useful reading.

The validation rules for the email id check are as follows (to be implemented in main()):

Were you specifically told to implement it "in main()"? Because if so, it's VERY bad advice.

My suggestion would be to set up a private method for each of your 4 validation checks, eg:
private final boolean checkLength(String email) { ...

2) The email ID must include “@” followed by a minimum of 1 and maximum of 2 “.”

Have a look at String's indexOf() method (there are a few variants), because this is what you'll need. You might also want to look at String.split() - a VERY useful method to know about.

Also: that validation actually involves TWO tests, so you might want to make it two methods.

Work some more on it, and see if you can't describe what you need to do, rather than write code for it. And feel free to come back with your description. We'll be more than happy to give you some more pointers.

Winston

PS: Your validateEmailId won't work properly because that's not how String.contains() works.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:And to that end, I have a few suggestions...

Actually, you could also do this with regular expressions; but I'd suggest trying to do it the old-fashioned way first. Then, if you have time, try tackling it with a regex.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:The validation rules for the email id check are as follows (to be implemented in main()):

Some more advice:
Make sure you understand exactly what is being asked of you; and if you're not sure, ask your tutor to clarify.
I'll give you some hints:

2) The email ID must include “@” followed by a minimum of 1 and maximum of 2 “.”
So, how many "@"s are allowed?

3) The substring before “@”must contain a combination of Upper case, Lower case and “_”
What about blanks?

4) The first letter of the Email ID must be in Upper Case
Have a look at the Character class (java.lang.Character (←click)). I think you'll find what you need. It'll also help with #3 above.

HIH

Winston
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
My suggestion would be to set up a private method for each of your 4 validation checks, eg:
private final boolean checkLength(String email) { ...


So, you are asking me to make separate method for each of these conditions. But what-if I want to make just a single method to validate all these conditions in one go.
Is it possible ?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Make sure you understand exactly what is being asked of you; and if you're not sure, ask your tutor to clarify.
I'll give you some hints:

2) The email ID must include “@” followed by a minimum of 1 and maximum of 2 “.”
So, how many "@"s are allowed?

3) The substring before “@”must contain a combination of Upper case, Lower case and “_”
What about blanks?

4) The first letter of the Email ID must be in Upper Case
Have a look at the Character class (java.lang.Character (←click)). I think you'll find what you need. It'll also help with #3 above.


Actually, I am undergoing an online generic training in Infosys company. I have been given a LabGuide which consists of several problems, and it was one of those.
I stated the problem as it is, and changed nothing.
In a general view the additional conditions you are talking about must also come to make complete sense of validating any email-address.

I have to come out with a single method which checks all these conditions in one go.
So please suggest me in that angle.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:So, you are asking me to make separate method for each of these conditions. But what-if I want to make just a single method to validate all these conditions in one go.
Is it possible ?

Sure, but the fact is that you need to tackle these problems separately, so whether you have 4 or 5, where the 5th is simply one that calls the other 4, only you can decide. The fact is that if you do it properly, you may well end up with more than that - and you'll also find that the problems can be combined to some extent, but you've got to think it through logically.

Like I say, stop thinking about code for the moment and describe how to solve the problem in English (or your native tongue).

Start with something like this:
"I have a String that might contain an e-mail address, and I need to validate it:
STEP 1: ....
STEP 2: ....
etc"

And don't use any Java keywords. Once you have the logic down, writing the code will be a doddle.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!