added field validation

This commit is contained in:
IvanaE 2018-10-21 16:34:47 -04:00
parent a1d92aa423
commit 4a725dd6d6
3 changed files with 48 additions and 28 deletions

View file

@ -20,21 +20,28 @@ public class LogIn extends AppCompatActivity {
String password = ((EditText) findViewById(R.id.PasswordInput)).getText().toString(); String password = ((EditText) findViewById(R.id.PasswordInput)).getText().toString();
DBHelper dbHelper = new DBHelper(this); DBHelper dbHelper = new DBHelper(this);
Intent intent = new Intent(getApplicationContext(),Welcome.class); Intent intent = new Intent(getApplicationContext(),Welcome.class);
if(dbHelper.findUserByUsername(username)!=null) { if(username.matches("[a-zA-Z0-9]*")&&password.matches("[a-zA-Z0-9]*")
if (dbHelper.findUserByUsername(username).getUsername().equals(username) && && password.length()>0 && username.length()>0) {
dbHelper.findUserByUsername(username).getPassword().equals(password)) { if (dbHelper.findUserByUsername(username) != null) {
intent.putExtra("username", username); if (dbHelper.findUserByUsername(username).getUsername().equals(username) &&
startActivity(intent); dbHelper.findUserByUsername(username).getPassword().equals(password)) {
intent.putExtra("username", username);
startActivity(intent);
} else {
Toast.makeText(this, "Wrong Password", Toast.LENGTH_LONG).show();
}
} else { } else {
Toast.makeText(this, "Wrong Password", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Account does not exist", Toast.LENGTH_LONG).show();
} }
} }
else{ else if(username.length()==0 || password.length()==0){
Toast.makeText(this, "Account does not exist", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Fields cannot be empty", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Fields may only contain alphanumeric values", Toast.LENGTH_LONG).show();
} }
} }

View file

@ -32,26 +32,39 @@ public class SignUp extends AppCompatActivity {
String lastname = ((EditText) findViewById(R.id.LastNameInput)).getText().toString(); String lastname = ((EditText) findViewById(R.id.LastNameInput)).getText().toString();
MaterialSpinner spinner = findViewById(R.id.RoleInput); MaterialSpinner spinner = findViewById(R.id.RoleInput);
//TODO add message conditional to check if every EditText is filled up to standards //TODO add message conditional to check if every EditText is filled up to standards
switch(spinner.getText().toString()){ if(username.length()>=5 && password.length()>5 && firstname.length()>0 && lastname.length()>0 && username.matches("[a-zA-Z0-9]*") && password.matches("[a-zA-Z0-9]*")
case "User": && firstname.matches("[a-zA-Z0-9]*") && lastname.matches("[a-zA-Z0-9]*")){
newUser = new User(username,password,firstname,lastname); switch(spinner.getText().toString()){
break; case "User":
case "Service Provider": newUser = new User(username,password,firstname,lastname);
newUser = new ServiceProvider(username,password,firstname,lastname); break;
break; case "Service Provider":
default: newUser = new ServiceProvider(username,password,firstname,lastname);
newUser = new User(username,password,firstname,lastname); //if nothing is enter then defaults to user role. break;
break; default:
newUser = new User(username,password,firstname,lastname); //if nothing is enter then defaults to user role.
break;
}
DBHelper dbHelper = new DBHelper(this);
Intent intent = new Intent(getApplicationContext(),LogIn.class); //TODO check if signup should take to the login page or automatically login
if(dbHelper.addUser(newUser)){
startActivityForResult(intent,0);
}else{
Toast.makeText(this,"Username is taken",Toast.LENGTH_LONG).show();
}
}
else if(firstname.length()==0 || lastname.length()==0 || username.length()==0 || password.length()==0){
Toast.makeText(this, "Fields cannot be empty", Toast.LENGTH_LONG).show();
}
else if (username.length()<=5 || password.length()<=5 ){
Toast.makeText(this, "Password and username must be longer than 5 characters", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "Fields may only contain alphanumeric values", Toast.LENGTH_LONG).show();
} }
DBHelper dbHelper = new DBHelper(this);
Intent intent = new Intent(getApplicationContext(),LogIn.class); //TODO check if signup should take to the login page or automatically login
if(dbHelper.addUser(newUser)){
startActivityForResult(intent,0);
}else{
Toast.makeText(this,"Username is taken",Toast.LENGTH_LONG).show();
}
} }

View file

@ -45,10 +45,10 @@
android:layout_height="50dp" android:layout_height="50dp"
android:layout_marginBottom="20dp" android:layout_marginBottom="20dp"
android:gravity="center" android:gravity="center"
android:text="You are logged in as" android:text="You are logged in as a"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="30sp" android:textSize="18sp"
app:fontFamily="@font/julius_sans_one" /> app:fontFamily="@font/julius_sans_one" />
<TextView <TextView