Jelajahi Sumber

added input validation (including email regex)

Noah 4 tahun lalu
induk
melakukan
40b66cbc16

+ 66 - 7
app/src/main/java/com/noahvogt/miniprojekt/MainActivity.java

@@ -1,5 +1,6 @@
 package com.noahvogt.miniprojekt;
 
+import android.annotation.SuppressLint;
 import android.os.Bundle;
 import android.view.View;
 import android.view.Menu;
@@ -22,6 +23,10 @@ import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;
 
+// regex utils for email string validation
+import android.util.Patterns;
+import java.util.regex.Pattern;
+
 public class MainActivity extends AppCompatActivity {
 
     private AppBarConfiguration mAppBarConfiguration;
@@ -78,40 +83,49 @@ public class MainActivity extends AppCompatActivity {
     }
 
     public void onClick(View view) {
-
-        // display snackbar message
-        Snackbar.make(view, "Mail has been SuCceSsFuLlY aSkEd FoR", Snackbar.LENGTH_LONG)
-                .setAction("Action", null).show();
+        // TODO: remove future button action ambiguity
         createNewEmailDialog();
     }
 
 
     public void createNewEmailDialog(){
+        // define View window
         dialogBuilder = new AlertDialog.Builder(this);
         final View emailPopupView = getLayoutInflater().inflate(R.layout.popup, null);
-        newemail_name = (EditText) emailPopupView.findViewById(R.id.popup_material_name_asking_text);
-        newemail_email = (EditText) emailPopupView.findViewById(R.id.popup_material_email_asking_text);
-        newemail_password = (EditText) emailPopupView.findViewById(R.id.popup_material_password_asking_text);
 
+        // init text field variables
+        newemail_name = emailPopupView.findViewById(R.id.popup_material_name_asking_text);
+        newemail_email = emailPopupView.findViewById(R.id.popup_material_email_asking_text);
+        newemail_password = emailPopupView.findViewById(R.id.popup_material_password_asking_text);
+
+        // init button variables
         newemail_save_button = (Button) emailPopupView.findViewById(R.id.saveButton);
         newemail_cancel_button = (Button) emailPopupView.findViewById(R.id.cancelButton);
 
+        // open View window
         dialogBuilder.setView(emailPopupView);
         dialog = dialogBuilder.create();
         dialog.show();
 
+        // store user input
         newemail_save_button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
+                // store user input (only needed for DEBUGGING)
                 String name = newemail_name.getText().toString();
                 String email = newemail_email.getText().toString();
                 String password = newemail_password.getText().toString();
 
+                if (!validateEmail() | !validateName() | !validatePassword()) {
+                    return;
+                }
+
                 // show all strings the user gave, this will later be stored to a secure database and checked for validation
                 showToast(name);
                 showToast(email);
                 showToast(password);
 
+
                 showSnackbar(emailPopupView,"save button clicked");
             }
         });
@@ -126,6 +140,51 @@ public class MainActivity extends AppCompatActivity {
 
     }
 
+    // TODO: resolve password endIcon conflict
+    private boolean validateName() {
+        String name = newemail_name.getText().toString().trim();
+
+        if (name.isEmpty()) {
+            newemail_name.setError("Field can't be empty");
+            return false;
+        } else if (name.length() > 50) {
+            newemail_name.setError("Name too long");
+            return false;
+        } else {
+            newemail_name.setError(null);
+            return true;
+        }
+    }
+
+    // TODO: resolve password endIcon conflict
+    private boolean validateEmail() {
+        String email = newemail_email.getText().toString().trim();
+
+        if (email.isEmpty()) {
+            newemail_email.setError("Field can't be empty");
+            return false;
+        } else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
+            newemail_email.setError("Please enter a valid email address");
+            return false;
+        } else {
+            newemail_email.setError(null);
+            return true;
+        }
+    }
+
+    // TODO: resolve password endIcon conflicts
+    private boolean validatePassword() {
+        String password = newemail_password.getText().toString().trim();
+
+        if (password.isEmpty()) {
+            newemail_password.setError("Field can't be empty");
+            return false;
+        } else {
+            newemail_password.setError(null);
+            return true;
+        }
+    }
+
     // show debug output in  specific view
     private void showSnackbar(View View, String text) {
         Snackbar.make(View, text, Snackbar.LENGTH_LONG)

+ 8 - 2
app/src/main/res/layout/popup.xml

@@ -12,6 +12,8 @@
         android:layout_height="150dp"
         android:background="@drawable/ic_baseline_face_24"
 
+        android:contentDescription="face icon"
+
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.498"
@@ -27,7 +29,6 @@
         android:layout_height="wrap_content"
 
         android:layout_marginStart="16dp"
-
         android:layout_marginLeft="16dp"
 
         android:layout_marginTop="16dp"
@@ -75,6 +76,9 @@
         android:hint="Your Name"
         app:startIconDrawable="@drawable/ic_person"
         app:endIconMode="clear_text"
+        app:counterEnabled="true"
+        app:counterMaxLength="50"
+        app:errorEnabled="true"
 
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
@@ -82,6 +86,9 @@
 
         <com.google.android.material.textfield.TextInputEditText
             android:id="@id/popup_material_name_asking_text"
+
+            android:inputType="textPersonName"
+
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />
     </com.google.android.material.textfield.TextInputLayout>
@@ -130,7 +137,6 @@
         android:hint="Your Password"
         app:startIconDrawable="@drawable/ic_lock"
         app:endIconMode="password_toggle"
-
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/popup_material_email_asking_layout">

+ 1 - 0
app/src/main/res/values/colors.xml

@@ -7,4 +7,5 @@
     <color name="teal_700">#FF018786</color>
     <color name="black">#FF000000</color>
     <color name="white">#FFFFFFFF</color>
+    <color name="error_red">#ff7400</color>
 </resources>

+ 15 - 0
app/src/main/res/values/style.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
+        <item name="colorAccent">#3498db</item>
+    </style>
+    <style name="error_appearance" parent="@android:style/TextAppearance">
+        <item name="android:textColor">@color/error_red</item>
+        <item name="android:textSize">12sp</item>
+    </style>
+
+    <style name="betterError" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox" >
+        <item name="errorTextColor">@color/error_red</item>
+        <item name="color">@color/error_red</item>
+    </style>
+</resources>