-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidation
95 lines (92 loc) · 2.69 KB
/
Validation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:minHeight="48dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp" />
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<TextView
android:id="@+id/textViewError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/my_red"
android:textSize="16sp" />
</LinearLayout>
MainActivity.java
package com.example.fourthprogram;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText emailEditText;
private EditText passwordEditText;
private Button submitButton;
private TextView errorTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = findViewById(R.id.editTextEmail);
passwordEditText = findViewById(R.id.editTextPassword);
submitButton = findViewById(R.id.buttonSubmit);
errorTextView = findViewById(R.id.textViewError);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateInputs();
}
});
}
private void validateInputs() {
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
// Reset error message
errorTextView.setText("");
if (TextUtils.isEmpty(email)) {
errorTextView.setText("Email cannot be empty");
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
errorTextView.setText("Invalid email address");
return;
}
if (TextUtils.isEmpty(password)) {
errorTextView.setText("Password cannot be empty");
return;
}
if (password.length() < 6) {
errorTextView.setText("Password must be at least 6 characters");
return;
}
Toast.makeText(MainActivity.this, "Validation Successful", Toast.LENGTH_SHORT).show();
}
}
colors.xml
<resources>
<color name="my_red">#FF0000</color>
</resources>