Home > DeveloperSection > Forums > Validation is not working and move to next activity in android
Ankit Singh

Total Post:341

Points:2389
Posted on    October-19-2014 11:28 PM

 Android Android  Validation 
Ratings:


 2 Reply(s)
 649  View(s)
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();

            }



Chintoo Semi

Total Post:135

Points:947
Supported
Posted on    October-19-2014 11:29 PM

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);
            }
         }
      });

Goti Bandu

Total Post:119

Points:835
Posted on    October-19-2014 11:31 PM

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
}

Don't want to miss updates? Please click the below button!

Follow MindStick