简体   繁体   中英

Android edittext how we can know user entered either phone or email

[login activity]

i am new to android app development. i am developing android app. user can login using email id or mobile no.like facebook login. how we can know either user entered email or mobile. please help me. For validation is their any validation frame work in android api like password,email id mobile no pattern matcher etc please help me.

<EditText
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:inputType="text"
    android:ems="10"
    android:id="@+id/etUserName"
    android:layout_marginTop="90dp"
    android:hint="Enter Email id/Mobile no"
    android:textColorHint="#4d060505"
    android:background="@drawable/edittext_bg"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:gravity="center">
    <requestFocus/>
</EditText>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:inputType="textPassword"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:ems="10"
    android:id="@+id/etPwd"
    android:hint="Password"
    android:textColorHint="#4d060505"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/edittext_bg"
    android:layout_below="@+id/etUserName"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:gravity="center"/>

this code worked for me perfectly. after your suggestions i tried it.

if(isEmailValid(loginname)){ Toast.makeText(getApplicationContext(), "Email", Toast.LENGTH_LONG).show(); }else if(isValidMobileNumber(loginname)){ Toast.makeText(getApplicationContext(), "mobileNo", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(getApplicationContext(), "Enter Email or Mobile No", Toast.LENGTH_LONG).show(); }

Try for Email

boolean isEmailValid(CharSequence email) {
 return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

if your Edittext value return true than its Email else Mobile number

You can use this to check whether the input is an email or not:

 public static boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

By These two methods I solved this problem

public static boolean isPhoneNumberValid(String phoneNoWithConCode, String countryCode)
    {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        try
        {
            Phonenumber.PhoneNumber numberProto = phoneUtil.parse(phoneNoWithConCode, countryCode);
            return phoneUtil.isValidNumber(numberProto);
        }
        catch (NumberParseException e)
        {
            System.err.println("NumberParseException was thrown: " + e.toString());
        }
        return false;
    }

public static boolean isEmailValid(String email){
    return Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

call these two methods one by one like

if(isPhoneNumberValid(.. , ..))
{
   //write your mobile no logic here
}else if(isEmailValid(..)
{
  //write you email logic here
}

First of all getText from editText and set proper validations for checking email and mobile number like below:

  email=edt_email.getText().toString();
  if (!isValidEmail(email)) 
  {
        // your code
  }

  private boolean isValidEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    private boolean isValidPhone(String pass) {
        return pass != null && pass.length() == 10;
    }

here are what I use in my project

private static boolean isValidPhoneNumber(String mobile) {
        String regEx = "^[0-9]{11,12}$";
        return mobile.matches(regEx);
    }

private boolean isValidEmaillId(String email){

        return Pattern.compile("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$").matcher(email).matches();
    }

you can use if else logic to it to validate mobile or email in your activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM