useful validation for android
Email:-
1) // default validation funtionality
if (!Patterns.EMAIL_ADDRESS.matcher(edt_email.getText().toString()).matches()) {
}
2) First charactor dont allow to "."(dot) or " "space.
edt_email.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override public void afterTextChanged(Editable s) {
try {
String inputChar = edt_email.getText().toString();
if (edt_email.getText().length() == 1) {
if (inputChar.equalsIgnoreCase(".") || inputChar.equalsIgnoreCase(" ")) {
edt_email.setText("");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
Mobile No.
1) First character allow only "+" sign.
edt_mobile.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override public void afterTextChanged(Editable s) {
try {
String last = edt_mobile.getText().toString();
String inputChar = last.substring(last.length() - 1);
if (edt_mobile.getText().length() > 1) {
if (inputChar.equalsIgnoreCase("+")) {
edt_mobile.setText(edt_mobile.getText().toString().substring(0, edt_mobile.getText().length() - 1));
edt_mobile.setSelection(edt_mobile.getText().length());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
2) Only allow particular digits (only allow declared digit or characters)
<string name="mobile_digits">+9876543210</string>
in xml
android:digits="@string/mobile_digits"