VALIDATION IS NOT WORKING AND MOVE TO NEXT ACTIVITY IN ANDROID

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 977  View(s)
Ratings:
Rate this:

Hi In My Application I have username and password and login button.click the login submit button without checking validations and empty fields also moving to next activity.

I want to check whether the username is required and password required clicking login button move to next activity.

Login.java

public class Login extends Activity {

    Button login;

    private static final Pattern USERNAME_PATTERN = Pattern

            .compile("[a-zA-Z0-9]{1,250}");

    private static final Pattern PASSWORD_PATTERN = Pattern

            .compile("[a-zA-Z0-9+_.]{4,16}");

    EditText usname,pword;

    TextView tv;

    String result=null;

    HttpPost httppost;

    StringBuffer buffer;

    HttpResponse response;

    HttpClient httpclient;

    CheckBox mCbShowPwd;

    List<NameValuePair> nameValuePairs;

    ProgressDialog dialog = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);

        login = (Button)findViewById(R.id.login);  

        usname = (EditText)findViewById(R.id.username);

        pword= (EditText)findViewById(R.id.password);

        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);

       mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                // checkbox status is changed from uncheck to checked.

                if (!isChecked) {

                        // show password

                    usname.setTransformationMethod(PasswordTransformationMethod.getInstance());

                } else {

                        // hide password

                     pword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                }

            }

        });

       login.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                final String username = usname.getText().toString();

                final String password = pword.getText().toString();

                if (username.equals("") || password.equals("")) {

                    if (username.equals("")) {

                        Toast.makeText(Login.this, "ENTER USERNAME",

                                Toast.LENGTH_LONG).show();

                    }

                    if (password.equals("")) {

                        Toast.makeText(Login.this, "ENTER PASSWORD",

                                Toast.LENGTH_LONG).show();

                    }

                } else {

                    if (!CheckUsername(username)) {

                        Toast.makeText(Login.this, "ENTER VALID USERNAME",

                                Toast.LENGTH_LONG).show();

                    }

                    if (!CheckPassword(password)) {

                        Toast.makeText(Login.this, "ENTER VALID PASSWORD",

                                Toast.LENGTH_LONG).show();

                    }

                }

                final String queryString = "username=" + username + "&password="

                        + password;

                String result = DatabaseUtility.executeQueryPhp("login",queryString);

                 Intent i = new Intent(getApplicationContext(), Home.class);

                    startActivity(i);

                        }

                      });

            }

            private boolean CheckPassword(String password) {

                return PASSWORD_PATTERN.matcher(password).matches();

            }

            private boolean CheckUsername(String username) {


                return USERNAME_PATTERN.matcher(username).matches();

            }

  1. Chintoo Semi

    Post:135

    Points:947
    Re: Validation is not working and move to next activity in android

    login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String username = usname.getText().toString();
                final String password = pword.getText().toString();
                if (username.equals("") || password.equals("")) {
                    if (username.equals("")) {
                        Toast.makeText(Login.this, "ENTER USERNAME",
                                Toast.LENGTH_LONG).show();

                    }
                    if (password.equals("")) {
                        Toast.makeText(Login.this, "ENTER PASSWORD",
                                Toast.LENGTH_LONG).show();

                    }

                } else if (!CheckUsername(username) && !CheckPassword(password)){
                        Toast.makeText(Login.this, "ENTER VALID USERNAME & PASSWORD",
                              Toast.LENGTH_LONG).show();
                }else{
                 Intent i = new Intent(getApplicationContext(), Home.class);
                    startActivity(i);
                }
             }
          });

  1. Goti Bandu

    Post:119

    Points:835
    Re: Validation is not working and move to next activity in android

    Use this to check username and same way check password

    public static boolean isValidUsername(EditText ed) {
        boolean isValidUsername = true;
        if (ed != null) {
            Pattern r = Pattern.compile(USERNAME_PATTERN);
            String s = ed.getText().toString().trim();
            String s1 = s.toString();
            Matcher m = r.matcher(s1);
            if (m.find()) {
                ed.setError("You are allowed to use a-z, A-Z, 0-9, ***") // *** are your                                 other possible symbols
                isValidUsername = false;
            } else if (s.length() < 5) {
                ed.setError("Atleast five characters must be there in username");
                isValidUsername = false;
            }
        }
        return isValidUsername;
    }
    And now

    if(editetxt_username.isValidUsernam() && editetxt_password.isValidPassword()) {
       //Change to new activity here
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!