jeudi 31 décembre 2015

Multiple IFs with one else

I have a validation procedure in which I have to check conditions one by one and then throw error messages accordingly. To achieve this, I am using multiple if statements, and if all the conditions are false, I should execute a task.

Here's my code:

if(s1.equals("") || s2.equals("") || s3.equals("") || s4.equals("") || s5.equals("") || s6.equals(""))      
    Toast.makeText(getApplicationContext(),"Enter all Details first", Toast.LENGTH_SHORT).show();       
if(!s5.matches("[A-Za-z0-9]+"))     
    e5.setError("Username cannot contain special characters");      
if(s5.length()<6)
    e5.setError("Username must be a minimum of 6 characters.");
if(!(s3.contains("@")&& s3.contains(".")))
    e3.setError("Enter a valid Email Id");      
if(!s6.equals(s7))
    e7.setError("Passwords dont match");
if(s2.length()!=10)     
    e2.setError("Please enter a valid 10 digit number");        
else
{
    Validate v2=new Validate();
    v2.store_values(s1,s2,s3,s4,s5,s6,s8);
    v2.execute();
}   

The problem is that the else statement is associates only with the last if statement, and the previous 5 if statements work independently. Please note that I cannot use else-ifs, since all the fields should be validated. How should I solve this problem?

Aucun commentaire:

Enregistrer un commentaire