Users Pricing

forum

home / developersection / forums / validation is not working and move to next activity in android

Validation is not working and move to next activity in android

Anonymous User 2168 19 Oct 2014

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


I am a content writter !


2 Answers

Anonymous User 19 Oct 2014 Accepted Answer