0

The below code is for a student registration application. For some reason the two validations shown first from the below code are not allowing the data to input into the database and then be fetched when logging in from the Main Activity Page.

Without these two lines the application works perfectly. Can someone tell me the issue?

Issues

            //issue
            if (!uname.getText().toString().matches("[c|C][0-9]{7}"))
            {uname.setError("Incorrect ID Format");}

            //issue
            if (!pass1.getText().toString().matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$"))
            {pass1.setError("Incorrect Password Format");}

SignUp.Java

package com.example.oliver.beckettreg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; 
import android.widet.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignUp extends Activity {

DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
}

public void onSignUpClick(View v)
{
    if(v.getId()== R.id.Bsignupbutton)
    {


        EditText name = (EditText)findViewById(R.id.TFname);
        EditText email = (EditText)findViewById(R.id.TFemail);
        EditText uname = (EditText)findViewById(R.id.TFuname);
        EditText pass1 = (EditText)findViewById(R.id.TFpass1);
        EditText pass2 = (EditText)findViewById(R.id.TFpass2);


        String namestr = name.getText().toString();
        String emailstr = email.getText().toString();
        String unamestr = uname.getText().toString();
        String pass1str = pass1.getText().toString();
        String pass2str = pass2.getText().toString();


        if(!pass1str.equals(pass2str))


        {
            //popup msg
            Toast pass = Toast.makeText(SignUp.this , "Passwords don't match!" , Toast.LENGTH_SHORT);
            pass.show();


        }
        else
        {

            if (name.getText().toString().length() == 0)
        {name.setError("Name Required");}

            if (!email.getText().toString().matches("[a-z]\\.[a-z]*[0-9]*@student\\.leedsbeckett\\.ac\\.uk"))
            {email.setError("Incorrect Email Format");}

            //issue
            if (!uname.getText().toString().matches("[c|C][0-9]{7}"))
            {uname.setError("Incorrect ID Format");}
            //issue
            if (!pass1.getText().toString().matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$"))
            {pass1.setError("Incorrect Password Format");}

            else{
            //insert the details in database
            Contact c = new Contact();
            c.setName(namestr);
            c.setEmail(emailstr);
            c.setUname(unamestr);
            c.setPass(pass1str);

            helper.insertContact(c);


                Toast pass = Toast.makeText(SignUp.this , "User Registered" , Toast.LENGTH_SHORT);
                pass.show();

        }

        }

    }


    }

public void onButtonClick(View v) {

    if (v.getId() == R.id.Blogin2) {
        Intent i = new Intent(SignUp.this, com.example.oliver.beckettreg.MainActivity.class);
        startActivity(i);

    }
}
}

Correct format for email would be a.aaaaaa8234@leedsbeckett.ac.uk

Correct format for uname would be c3400554

Correct format for password would be:

  • a digit must occur at least once
  • a lower case letter must occur at least once
  • an upper case letter must occur at least once
  • a special character must occur at least once
  • no whitespace allowed in the entire string
  • anything, at least eight places
oliver_13
  • 39
  • 10

2 Answers2

1

If you put your regex code into it's own class, you could easily test it to make sure that it works before using it in the context of your user interface code.

class Validator {

    public boolean isValidUserName(String username) {
        return username.matches("[c|C][0-9]{7}");
    }
}

Then a unit test:

class ValidatorTest {

    @Test
    testUsernameValidator() {
        String username = "someName";
        Validator validator = new Validator();
        Assert.assertTrue(validator.isValidUsername(username));
    }
}

This would free you from testing both the UI and the validator at runtime.

Matt
  • 3,677
  • 1
  • 14
  • 24
1

For your username use it like: uname.getText().toString().matches(/[c|C][0-9]{7}/)

For email this is a classic:

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

From: Validate email address in JavaScript?

For password:

  function checkPassword(pass)
  {
    var re = /(?=.*[\d])(?=.*[a-z])(?=.*[A-Z]).{8,}/;
    return re.test(pass);
  }
Community
  • 1
  • 1
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • The username and email both work for me but I am struggling with the password, what validations does the regular expression you show make? – oliver_13 Mar 30 '16 at 01:08