0

I'm trying to make a registration form of an Android application. I've made a database adapter and such using MySQLite.

The problem that I've been facing is the validations of the entries inputted into the EditText controls. When I click on Register button, the information should be stored in a Database, making sure that the entries are not empty, the confirm password matches the password, and most importantly is that the username is not already used. I've tried every single way to do so but it's either that I get a runtime force close error, or the validations are not working the way I want to.

    final EditText unedit = (EditText) findViewById(R.id.unedit);
    final EditText passedit = (EditText) findViewById(R.id.passedit);
    final EditText conpassedit = (EditText) findViewById(R.id.conpassedit);
    final EditText nameedit = (EditText) findViewById(R.id.nameedit);
    final EditText emailedit = (EditText) findViewById(R.id.emailedit);
    final EditText mobedit = (EditText) findViewById(R.id.mobedit);

    Button register = (Button) findViewById(R.id.registerbutton);

    register.setOnClickListener(new View.OnClickListener() {

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

            String username = unedit.getText().toString();
            String password = passedit.getText().toString();
            String conpassword = conpassedit.getText().toString();
            String name = nameedit.getText().toString();
            String email = emailedit.getText().toString();
            String mobileno = mobedit.getText().toString();

            RegDb reg = new RegDb(Register.this);
            reg.open();
            String returnedUn = reg.getUn(username);
            reg.close();

            if (username.isEmpty()) {
                unedit.setError("Username is required");
                // return;
            }
            if (password.isEmpty()) {
                passedit.setError("Password is required");
                // return;
            }
            if (conpassword.isEmpty()) {
                conpassedit.setError("Password confirmation is required");
                // return;
            }
            if (name.isEmpty()) {
                nameedit.setError("Name is required");
                // return;
            }
            if (email.isEmpty()) {
                emailedit.setError("Email is required");
                // return;
            }
            if (mobileno.isEmpty()) {
                mobedit.setError("Mobile No is required");
                // return;
            }
            if (username.equals(returnedUn)) {
                unedit.setError("Username is already used");
                // return;
            }
            if (!conpassword.equals(password)) {
                passedit.setError("Check your password");
                // return;
            }
            if (!username.isEmpty() && !password.isEmpty()
                    && !name.isEmpty() && !email.isEmpty()
                    && !mobileno.isEmpty() && conpassword.equals(password)
                    && !username.equals(returnedUn)) {
                Dialog d = new Dialog(v.getContext());
                d.setTitle("Account created.");
                d.show();
            } else {
                return;
            }

I also used the try-catch-finally, but the validations can't seem to work neither.

 boolean created = true;

                try {

                    if (!username.isEmpty() && !password.isEmpty()
                    && !name.isEmpty() && !email.isEmpty()
                    && !mobileno.isEmpty() && conpassword.equals(password)
                    && !username.equals(returnedUn)){
                    RegDb entry = new RegDb(Register.this);
                    entry.open();
                    entry.createEntry(username, password, name, email,
                            mobileno);

                    entry.close();
                }

            } catch (Exception e) {
                created=false;
                Dialog d = new Dialog(v.getContext());
                d.setTitle("Please check your entries.");
                d.show();

            } finally {
                if (created) {
                    Dialog d = new Dialog(v.getContext());
                    d.setTitle("Account created.");

                    Intent i = new Intent(v.getContext(),
                            MainActivity.class);
                    startActivity(i);

                }
            }

Please enlighten me with what I'm doing wrong and what should be done.

1 Answers1

0

First of all you do not need to declare this way final EditText unedit = (EditText) findViewById(R.id.unedit);

you can declare them at class level as

private EditText unedit;

Now on onCreate() method access it using following way,

unedit = (EditText) findViewById(R.id.unedit);

and check the string variable as follows,

String username = unedit.getText().toString().trim();  // Add Trim() method

if (username.equals("")) {
                unedit.setError("Username is required");
                // return;
            }

same for all other.

Raynold
  • 443
  • 2
  • 9
  • 28
  • Thanks! I will consider that. But what if I wanted to proceed with storing the values into the database, how can I validate that all of the strings are not empty, the username does not exist in the database and the confirm password is equal to password? – RoseofMay-x Mar 29 '13 at 13:08
  • just check the string before storing into db. – Raynold Mar 29 '13 at 13:10