5

I am trying to figure out if a user has entered an email id or a phone number. Therefore i would like to check if the string starts with +1 or a number to determine if it is a phone number . If it is not either i come to the conclusion it is an email or i could check if it starts with a alphabet to be sure. How do i check this . I am horrible with regex if that is the soln .

vijar
  • 733
  • 4
  • 14
  • 23
  • 2
    Why are they being put into the same field? – Jack Jan 27 '14 at 22:29
  • If you know it will always be one or the other, then check if it contains a `@`. If yes then email, otherwise phone number. – Matt Jan 27 '14 at 22:30

4 Answers4

7

You can do this with RegEx, but a simple if statement will work as well, and will likely be more readable. If an @ character is not present in the string and the first character is a number, it is reasonable to assume it's a phone number. Otherwise, it's likely an email address, assuming an @ is present. Otherwise, it's likely invalid input. The if statement would look like this:

if(yourString.indexOf("@") < 0 && !isNaN(+yourString.charAt(0) || yourString.charAt(0) === "+")) {
    // phone number
} else if(yourString.indexOf("@") > 0) {
    // email address
} else {
    // invalid input
}
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • *"If an `@` character is not present in the string and it is a number..."* Phone numbers often include non-numeric characters, like `(` and `)` or `ext 1324`. – T.J. Crowder Jan 27 '14 at 22:33
  • This is the most suited answer for me as it solves my current problem . Thanks – vijar Jan 27 '14 at 22:33
  • Hmm, true, @T.J.Crowder. A phone number input validation library (or very long RegEx) would probably be the best bet to solve this issue. However, we can just check the first character of the phone number for now, I suppose. – Elliot Bonneville Jan 27 '14 at 22:34
  • Validating phone numbers is almost always a fool's errand. I'd make them separate inputs. If they **have** to be one input (because some high-up is being a twit), just the presence of the `@` would be enough for me. – T.J. Crowder Jan 27 '14 at 22:36
  • Because you only parse the first character, this doesn't handle the OP's case where the phone number starts with "+1". – ty. Jan 27 '14 at 22:37
  • That's really quite true, @T.J.Crowder. I guess I'll just stick with an implementation of the OP's logic. **ty**, you're correct. Editing to fix. – Elliot Bonneville Jan 27 '14 at 22:39
4
if (!isNaN(parseInt(yourstrung[0], 10))) {
  // Is a number
}
Nick
  • 4,192
  • 1
  • 19
  • 30
3

Just do the following:

if ( !isNaN(parseInt(inputString)) ) {
    //this starts with either a number, or "+1"
}
Joeytje50
  • 18,636
  • 15
  • 63
  • 95
1

Might I suggest a slightly different approach using the regex email validation found here?

if(validateEmail(input_str)) {
    // is an email
} else if(!isNaN(parseInt(input_str))) {
    // not an email and contains a number
} else {
    // is not an email and isn't a number
}

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

This way you can check a little more thoroughly on what the input actually is, rather than just guessing it's one or the other.

Community
  • 1
  • 1
Samsquanch
  • 8,866
  • 12
  • 50
  • 89