'Edittext validation in Android Studio
Hello hope that you are fine would like to get an help on how i can validate the Edittetxt make sure that the phone number entered is correct and the password should also match before i submit the data i have seen couple of examples but i didn't get the actual concept on how the whole thing works.
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:paddingTop="16dp"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/penta_ujumbe"
android:text="@string/penta_join"
android:textSize="16sp"
android:layout_marginTop="16dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="90dp" />
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:layout_gravity="center_horizontal"
android:id="@+id/penta_jina"
android:hint="Jina lako"
/>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_gravity="center_horizontal"
android:id="@+id/penta_simu"
android:hint="Namba Ya Simu"
android:digits="1,2,3,4,5,6,7,8,9,0,+"
/>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_gravity="center_horizontal"
android:id="@+id/penta_siri"
android:hint="NenoSiri"
/>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="280dp"
android:layout_gravity="center_horizontal"
android:id="@+id/penta_nsiri"
android:hint="Rudia Nenosiri"
/>
<Button
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:id="@+id/penta_fungua"
android:layout_below="@+id/pik_siri"
android:layout_marginTop="330dp"
android:layout_gravity="center_horizontal"
android:text="Fungua akaunti"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="@color/colorWhite"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
The codes in the Activity are as follows the problem is that the application crashes a lot.
private boolean validate(){
boolean valid = true;
String mjina = pikujina.getText().toString();
String mnamba = pikunamba.getText().toString();
String msiri = pikusiri.getText().toString();
String mnsiri = pikunsiri.getText().toString();
if (mjina.isEmpty() || mjina.length() < 3) {
pikujina.setError("at least 3 characters");
valid = false;
return valid;
}
if (mnamba.isEmpty() || !isPhoneNumberValid(mnamba)) {
pikunamba.setError("enter a valid phone number with your country code");
valid = false;
return valid;
}
if (msiri.isEmpty() || msiri.length() < 4 || msiri.length() > 10) {
pikunsiri.setError("between 4 and 10 alphanumeric characters");
valid = false;
return valid;
}
if (mnsiri.isEmpty() || mnsiri.length() < 4 || mnsiri.length() > 10) {
pikunsiri.setError("between 4 and 10 alphanumeric characters");
valid = false;
return valid;
}
if (!msiri.equals(mnsiri)){
pikunsiri.setError("Password do not Match");
pikunsiri.setError("Password do not Match");
valid = false;
return valid;
}
else {
return valid;
}
}
public static boolean isPhoneNumberValid(String phoneNumber) {
boolean valid = true;
String regex = "^(?:00255|\\+255|0)[6-9][0-9]{9}";
if (!phoneNumber.matches(regex)) {
valid = false;
}
return valid;
}
AND I CALL THIS FUNCTION USING AN IF CONDITION INSIDE THE ONCLICK FUNCTION.
public void onClick(View v) {
if(validate()){
Toast.makeText(piku_join.this,"Entered"+pikunamba+"and password entered is",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(piku_join.this,"Failed"+pikunamba+"and password entered is",Toast.LENGTH_SHORT).show();
}
Solution 1:[1]
here i mentioned some edittext validation check it out, I hope it will help u:
private boolean validate() {
boolean valid = true;
String name = et_username.getText().toString();
String email = et_email.getText().toString();
String phone = et_phone.getText().toString();
String password = et_password.getText().toString();
if (name.isEmpty() || name.length() < 3) {
et_username.setError("at least 3 characters");
valid = false;
} else {
et_username.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
et_email.setError("enter a valid email address");
valid = false;
} else {
et_email.setError(null);
}
if (phone.isEmpty() || !isPhoneNumberValid(phone)) {
et_phone.setError("enter a valid phone number with your country code");
valid = false;
} else {
et_phone.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
et_password.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
et_password.setError(null);
}
return valid;
}
public static boolean isPhoneNumberValid(String phoneNumber) {
boolean valid = true;
String regex = "^(?:0091|\\+91|0)[7-9][0-9]{9}";
if (!phoneNumber.matches(regex)) {
valid = false;
}
return valid;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Manish |
