0

These 3 contact information (Home No., Mobile No. & Email Address) should be filled out at least one. If the user enters a wrong information for one of these contact information, the trapping code will be executed.

But unfortunately, my codes doesn't working...

 else if ((Homeno.equals("")) || (MobileNo.equals("")) || (Email.equals("")) ) { Toast.makeText(getApplicationContext(), "Please enter at least one (1) your contact information.", Toast.LENGTH_SHORT).show(); } //TRAPPING CODES //Tel no. Validator else if (Homeno.length()<9) { Toast.makeText(getApplicationContext(), "Invalid Telephone Number!", Toast.LENGTH_SHORT).show(); } //Mobile Number must be at least 12 else if (MobileNo.length()<12) { Toast.makeText(getApplicationContext(), "Mobile Number must be 12 digits!", Toast.LENGTH_SHORT).show(); } //Mobile Validator else if (!validcp.matches()) { Toast.makeText(getApplicationContext(), "Invalid Mobile Number!", Toast.LENGTH_SHORT).show(); } //Email validator else if (!matcherObj.matches()) { Toast.makeText(getApplicationContext(), "Invalid Email Address!", Toast.LENGTH_SHORT).show(); } 
4
  • What toast message do you see after running the code? Commented May 30, 2013 at 1:01
  • need more information, logcat error log (if any exceptions are being thrown) also have you tried placing breakpoints in the else if statement to make sure they are being entered? Commented May 30, 2013 at 1:01
  • 22kar, only "Please enter at least one (1) your contact information." Commented May 30, 2013 at 1:19
  • Why are you starting your first condition with else if? Are there any other if statements before that one? Commented May 30, 2013 at 3:50

1 Answer 1

2

Firstly you say that code does not work. please be specific. You say that Out of 3 contact information (Home No., Mobile No. & Email Address) should be filled out at least one. From what you've said I understand that user should be able to enter any one of them and leave the other 2 blank. But, acording to above statement:

else if ((Homeno.equals("")) || (MobileNo.equals("")) || (Email.equals("")) ) {} 

it means that if any one of them is empty, your app will show the toast:

Please enter at least one (1) your contact information( even though any one is entered).

If any one is empty, the control won't go inside any other if conditions that is following the above statement( as you've used else if). Hope this helps. This is one of the things you might want to look at. For other problems please be specif and we can try to help you.

Update You should have something like:

... else if ((Homeno.equals("")) && (MobileNo.equals("")) && (Email.equals("")) ) { Toast.makeText(getApplicationContext(), "Please enter at least one (1) your contact information.", Toast.LENGTH_SHORT).show(); } 

In fact there might be one more problem which I can think of:

If you have four fields then I believe you would want to check the validity of all four of them. So, you should not use else if for validating conditions. Instead you might want to check all four.

Use the whole thing like:

... }else if ((Homeno.equals("")) && (MobileNo.equals("")) && (Email.equals("")) ) { Toast.makeText(getApplicationContext(), "Please enter at least one (1) your contact information.", Toast.LENGTH_SHORT).show(); } else{ if (Homeno.length()<9) { Toast.makeText(getApplicationContext(), "Invalid Telephone Number!", Toast.LENGTH_SHORT).show(); } //Mobile Number must be at least 12 if ( (MobileNo.length()<12) { Toast.makeText(getApplicationContext(), "Mobile Number must be 12 digits!", Toast.LENGTH_SHORT).show(); } //Mobile Validator if (!validcp.matches()) { Toast.makeText(getApplicationContext(), "Invalid Mobile Number!", Toast.LENGTH_SHORT).show(); } //Email validator if (!matcherObj.matches()) { Toast.makeText(getApplicationContext(), "Invalid Email Address!", Toast.LENGTH_SHORT).show(); } } 

Hope it answers all your queries. If you have other concerns or you didn't get something, you can comment below.

Update 2:

Based on suggestion given by @Nicholas , I've updated the answer. The four if clauses will execute only if atleast one of them is not empty.

Sign up to request clarification or add additional context in comments.

6 Comments

I stand corrected in the AND clauses... Thanks Shobhit Puri. However, your suggestion to change my "else if's" to "if's" for validating conditions doesn't working. It toasts all messages from the if's consecutively...
When does that occur? I mean for what input to those four fields you get all the toast messages? Are all of them valid?
@PaulMarcellana : See updated answer. The four if clauses will execute only if atleast one of them is not empty. Hope this helps. If it does not work, do tell the use cases for which its not working. It will help see the problem.
I figure it out already! Thanks for your creative answer @ShobhitPuri. :)
else if ((Homeno.equals("")) && (MobileNo.equals("")) && (Email.equals("")) ) { //Toast Message... }
|