4

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,

enter image description here

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
anafroz
  • 71
  • 2
  • 9
  • In If statement check that text is Email Id in else if statement check it's a phone number or not use else based on your needs that's enough – MathankumarK Mar 21 '18 at 13:18
  • i need to check some condition 1) if user give character means, we check its valid email address or not? 2) if user give number means, we check length greater 6 or not? . so first we need to check whether given string is number or alphabet – anafroz Mar 21 '18 at 13:26

5 Answers5

4

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();
            }
        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
2

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 
}
  • i need to check some condition 1) if user give character means, we check its valid email address or not? 2) if user give number means, we check length greater 6 or not? . so first we need to check whether given string is number or alphabet – anafroz Mar 21 '18 at 13:24
  • What are the conditions, state your goal so we can help you – Amine ABDALKHALKI Mar 21 '18 at 13:26
  • i have changed my answer, the method IsValidMobile and IsValidEmail will help you to check the validation of the value entered by the user either way (no need to check the lenght or not), if one of the test is validated you will check if it's numeric or not in order to show the specific image you want – Amine ABDALKHALKI Mar 21 '18 at 13:46
1

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
}
godot
  • 3,422
  • 6
  • 25
  • 42
  • i need to check some condition 1) if user give character means, we check its valid email address or not? 2) if user give number means, we check length greater 6 or not? . so first we need to check whether given string is number or alphabet – anafroz Mar 21 '18 at 13:26
  • this builtin Patterns would check what you want. – godot Mar 21 '18 at 13:28
0

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 "."

kfir88
  • 380
  • 2
  • 16
  • i need to check some condition 1) if user give character means, we check its valid email address or not? 2) if user give number means, we check length greater 6 or not? . so first we need to check whether given string is number or alphabet – anafroz Mar 21 '18 at 13:26
0

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.

costa
  • 19
  • 3