1

I have a situation where user will enter either email or phone number for login into app,only one edittext is provided , like in paytm

I need to detect which is entered either email or phone no so that i can set my server correctly how can i detect what user has entered number or email

Pragadees
  • 71
  • 1
  • 12
  • 1
    If it doesn't contain `@` it's a phone number. But I think that a better practice will be to display both fields with "or" in between and let the user decide if she wants to fill out the phone, email or both. – Nir Alfasi Oct 07 '15 at 06:36
  • if you're on android, do it the android way :) That's why I like @wblaschko 's answer – Bö macht Blau Oct 07 '15 at 06:48

3 Answers3

3

One thing you could do is check if it's an email address and then check whether it's a valid phone number. I would use the built in Android function like so:

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

If that returns false, check whether it's a valid phone number (Patterns.PHONE).

If one or the other returns true, then you know which it is. If neither is true, it is an invalid entry.

More on email and phone matching.

wblaschko
  • 3,252
  • 1
  • 18
  • 24
0

A pair of regular expressions...

For email: http://www.regular-expressions.info/email.html

For phone: A comprehensive regex for phone number validation

A simpler approach would be exactly like you might think:

  1. Trim the leading and trailing whitespace

  2. Look for the @ char in between any other pair of strings - if so, assume it's an email.

  3. Otherwise, do some rudimentary removing of parentheses, dashes, whitespace and dots. If what's left is all numbers, it's probably a phone number.

No matter how you parse and detect the string as email vs. phone number, somewhere in the sequence you have to validate if these strings are legitimate by either dialing it or mailing something to it.

Community
  • 1
  • 1
selbie
  • 100,020
  • 15
  • 103
  • 173
0

Check out this link Email and phone Number Validation in android

you can set condition if Mobile of Email is valid.

Community
  • 1
  • 1