-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainActivity.java
More file actions
123 lines (106 loc) · 4.08 KB
/
MainActivity.java
File metadata and controls
123 lines (106 loc) · 4.08 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Author: Amey Thakur
Course: Mobile Communication and Computing (MCC) & Mobile Application Development Lab (MAD Lab)
Semester: VII | Batch: B3 | Roll No: 50
Experiment: 9 - Android User Form with Validation
Date: October 01, 2021
GitHub: https://github.com/Amey-Thakur
Repository: https://github.com/Amey-Thakur/MOBILE-COMMUNICATION-AND-COMPUTING-AND-MOBILE-APPLICATION-DEVELOPMENT-LAB
*/
package android.demo.learnandroidwithrealapps;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Checked;
import com.mobsandgeeks.saripaar.annotation.ConfirmPassword;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.Length;
import com.mobsandgeeks.saripaar.annotation.Max;
import com.mobsandgeeks.saripaar.annotation.Min;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Pattern;
import com.mobsandgeeks.saripaar.annotation.Url;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
Validator.ValidationListener {
@NotEmpty
@Length(min = 3, max = 10)
private EditText editTextUsername;
@NotEmpty
@Password
@Pattern(regex = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})")
private EditText editTextPassword;
@ConfirmPassword
private EditText editTextConfirmPassword;
@NotEmpty
@Pattern(regex = "^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$")
private EditText editTextPhone;
@Min(18)
@Max(120)
private EditText editTextAge;
@NotEmpty
@Email
private EditText editTextEmail;
@Url
private EditText editTextWebsite;
@Checked
private CheckBox checkBoxAgree;
private Button buttonSave;
private Validator validator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
validator = new Validator(this);
validator.setValidationListener(this);
}
private void initView() {
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
editTextConfirmPassword = findViewById(R.id.editTextConfirmPassword);
editTextAge = findViewById(R.id.editTextAge);
editTextPhone = findViewById(R.id.editTextPhone);
editTextEmail = findViewById(R.id.editTextEmail);
editTextWebsite = findViewById(R.id.editTextWebsite);
checkBoxAgree = findViewById(R.id.checkBoxAgree);
buttonSave = findViewById(R.id.buttonSave);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonSave_onClick(view);
}
});
}
private void buttonSave_onClick(View view) {
validator.validate();
String username = editTextUsername.getText().toString();
if (username.equalsIgnoreCase("pmk")) {
editTextUsername.setError(getText(R.string.username_already_exists));
}
}
@Override
public void onValidationSucceeded() {
Toast.makeText(this, "We got it right!", Toast.LENGTH_SHORT).show();
}
@Override
public void onValidationFailed(List<ValidationError> errors) {
for (ValidationError error : errors) {
View view = error.getView();
String message = error.getCollatedErrorMessage(this);
// Display error messages
if (view instanceof EditText) {
((EditText) view).setError(message);
} else {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
}
}