Wednesday 27 February 2013

Login Page Validations




Code:

            EditText edtEmail = (EditText) findViewById(R.id.edtEmail);
            EditText edtPassword = (EditText) findViewById(R.id.edtPassword);

            Button btnLogin = (Button) findViewById(R.id.btnLogIn);

            btnLogin.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        String email = edtEmail.getText().toString().trim();
                        String password = edtPassword.getText().toString().trim();

                       
                        // Validatation part here

                        if (email.matches("")) {

                              edtEmail.setError("Enter Email Id");
                              edtPassword.setText("");

                              Toast.makeText(getApplicationContext(),
                                          "Please Enter Email Id", Toast.LENGTH_SHORT).show();

                        } else if (!(email
                                    .matches("[a-zA-Z0-9._-]+@[a-zA-Z]+\\.+[a-zA-Z]+"))) {

                              edtEmail.setError("Enter valid Email id");

                              edtEmail.setText("");
                              edtPassword.setText("");

                              Toast.makeText(getApplicationContext(),
                                          "Please Enter valid Email id", Toast.LENGTH_SHORT)
                                          .show();

                        } else if (password.length() < 5) {

                              edtPassword.setError("Enter Correct Password");

                              edtPassword.setText("");

                              Toast.makeText(getApplicationContext(),
                                          "Please Enter Correct Password", Toast.LENGTH_SHORT)
                                          .show();

                        } else {

                              //Success Logic
                              Toast.makeText(getApplicationContext(),
                                          email + "<=>" + password, Toast.LENGTH_SHORT)
                                          .show();
                        }

                  }
            });




No comments:

Post a Comment