Reputation: 23
[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();
}
Upvotes: 1
Views: 1864
Reputation: 935
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
}
Upvotes: 1
Reputation: 95
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
Upvotes: 0
Reputation: 9225
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;
}
Upvotes: 0
Reputation: 4328
Try for Email
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
if yourEdittext
value return true than its Email else Mobile number
Upvotes: 3
Reputation: 4702
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();
}
Upvotes: 2