Login form validation in android

Hi i have developed one login form in android.here i have use validation here.here i have to fill anyone(username or password) means my app is successfully displayed login fail and does not move to other activity.but the both field is empty means it is displayed success but does not move to other activity.but i wish to need both field empty means the app is displayed login fail...please help me hoe can i develop this.

this is my webservice code:

public class XcartLogin {
public String authentication(String userName,String password){

 String retrievedUserName = "";
 String retrievedPassword = "";
 String status = "";
 try{

   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xcart432-pro","root","");
     PreparedStatement statement =  con.prepareStatement("SELECT * FROM xcart_customers WHERE login = '"+userName+"'");
      ResultSet result = statement.executeQuery();

       while(result.next()){
     retrievedUserName = result.getString("login");
    retrievedPassword = result.getString("password");
     }

   if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){
    status = "Success!";

    }

     else{
      status = "Login fail!!!";
     }

        }
      catch(Exception e){
        e.printStackTrace();
       }
      return status;

         }

        }

this is validation for my android code:

 if(status.equals("Success!"))
               {

                   //   ADD  to save  and  read next time
                       String strUserName = userName.getText().toString().trim();
                       String strPassword = userPassword.getText().toString().trim();

                       if (null == strUserName || strUserName.length() == 0)
                                   {
                           //  showToast("Enter Your Name");
                         userName.setError( "username is required!" );
isUserValidated = false;
                       }
  if (null == strPassword || strPassword.length() == 0)
                                   {
                               //      showToast("Enter Your Password");
isPasswordValidated = false;
                         userPassword.setError( "password is required!" );
                       } 

please help me...if both field is empty means the success message is not displayed here.

Answers


try using

   if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)&&!(retrievedUserName.equals("") && retrievedPassword.equals("")))

instead of

 if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)) 

Before the if(status.equals("Success!")) statement in your validation code, you should do this first to avoid querying the database if any of the text fields are empty in the first place:

boolean errorOccurred = false;
if (strUserName.equals("")) {
    userName.setError("Username is required!");
    errorOccurred = true;
}

if (strPassword.equals("")) {
    userName.setError("Password is required!");
    errorOccurred = true;
}

if (errorOccurred) {
    return; // avoids executing the part of your code which queries the db
}

Checking for whether the input fields' values are null is quite pointless since if they don't contain anything, it will only be an empty string, or "". Then, to simplify your webservice code...

if (result.next()) { // use if instead of while, because ideally, only ONE record should
                     // be returned and hence, no need to loop;

    // then, just get the corresponding password
    retrievedPassword = result.getString("password");
}

if (retrievedPassword.equals(password)) {
    status = "Success!";
}

Further suggestion: put "Success!" in a String constant and use that instead of the literal value. You have lesser chances of committing mistakes this way and makes editing your code easier.


Need Your Help

Form processing won't pass value

php mysql

i have this code which permits me to do a request in order to make a query!

Testing code dependant on existence of class on classpath

java unit-testing maven classpath

I have a project where I need to provide SLF4J logging if SLF4J is on the classpath and otherwise provide logging straight to the console. I instantiate my logger with code similar to:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.