Reputation: 71
In my app login using mobile number or phone number in single edittext. if we give digits it check valid phone number else if we give character(alphabet) check it is valid email address.
Note : check both email and phone number in single edit text.
Here java code,
if (username.getText().toString().length() == 0) {
erroredit(username, getResources().getString(R.string.login_label_alert_username));
} else if (!isValidEmail(username.getText().toString().replace(" ","").trim())) {
erroredit(username, getResources().getString(R.string.login_label_alert_email_invalid));
} else if (!isValidmobilenumber(username.getText().toString().replace(" ","").trim())) {
erroredit(username, getResources().getString(R.string.register_label_alert_phoneNo));
}
else if (password.getText().toString().length() == 0) {
erroredit(password, getResources().getString(R.string.login_label_alert_password));
}
Attached screenshot here,
Upvotes: 4
Views: 4329
Reputation: 19
If you want real time checking while user is writing then you can have a TextWatcher How to use the TextWatcher class in Android?
Do your checks in the afterTextChanged override method with android.util.Patterns.PHONE
or an equivalent like the user Godot explained above.
Upvotes: 0
Reputation: 445
you can combine codes to achieve that, first add the following methods:
private boolean isValidMobile(String phone) {
boolean check=false;
if(!Pattern.matches("[a-zA-Z]+", phone)) {
if(phone.length() < 6 || phone.length() > 13) {
// if(phone.length() != 10) {
check = false;
txtPhone.setError("Not Valid Number");
} else {
check = true;
}
} else {
check=false;
}
return check;
}
and for email validatio add this one:
private boolean isValidMail(String email) {
boolean check;
Pattern p;
Matcher m;
String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
p = Pattern.compile(EMAIL_STRING);
m = p.matcher(email);
check = m.matches();
if(!check) {
txtEmail.setError("Not Valid Email");
}
return check;
}
also this is needed to check if the string numeric or not
public static boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
}
in your code you call those function like
if(isValidMobile(login) || isValidMail(login){
// success
if(isNumeric(login)){
//it mean the user typed a phone number
//show phone invalid image
}
else{
//show email invalid image
}
}
else {
// failure
}
Upvotes: 2
Reputation: 69709
Try this
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editemail.getText().toString().trim();
String pass = edtPass.getText().toString().trim();
boolean flag = false;
if (TextUtils.isEmpty(pass)) {
edtPass.setError("Enter password");
//edtPass.requestFocus();
}else {
}
if (TextUtils.isEmpty(data)) {
editemail.setError("Enter Data");
} else {
if (data.matches("[0-9]+")) {
if (data.length() < 10 && data.length() > 10) {
editemail.setError("Please Enter valid phone number");
editemail.requestFocus();
} else {
flag = true;
}
} else {
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(data).matches()) {
editemail.setError("Please Enter valid email");
editemail.requestFocus();
}else {
flag = true;
}
}
}
if(!TextUtils.isEmpty(pass)&&flag){
Toast.makeText(MainActivity.this, "pass", Toast.LENGTH_SHORT).show();
}
}
});
Upvotes: 4
Reputation: 3545
assume your EditText
variable is editText
:
String username = editText.getText().toString();
if(android.util.Patterns.EMAIL_ADDRESS.matcher(username).matches()
||
android.util.Patterns.PHONE.matcher(username).matches()){
//do stuff
}
else{
//do stuff
}
Upvotes: 1
Reputation: 380
I very often use this dependency when it comes to manipulating phone numbers. It's very useful.
For email it's not complicated just check the character "@" and "."
Upvotes: 0