Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to identify if string is a phone number, Email address or a web address in Javascript?

How to identify if string is a phone number, Email address or a web address in Javascript?

Scheduled Pinned Locked Moved JavaScript
javascriptcomsalesjsontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13942898
    wrote on last edited by
    #1

    I am calling a third-party API to get customer contact information and it returns a string for each field and my application needs to determine if the string is a phone number or Email or web address

    String data=GetCustomerInfoFromAPI() // will get customer contact information from an API call and will return a string
    

    some examples for the data string I am getting back from the API call
    "www.google.com"
    "602.123.1778"
    "John@gmail.com"

    L B 2 Replies Last reply
    0
    • U User 13942898

      I am calling a third-party API to get customer contact information and it returns a string for each field and my application needs to determine if the string is a phone number or Email or web address

      String data=GetCustomerInfoFromAPI() // will get customer contact information from an API call and will return a string
      

      some examples for the data string I am getting back from the API call
      "www.google.com"
      "602.123.1778"
      "John@gmail.com"

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If the source system does not provide the type then you will need to work it out by trial and error. Assuming that each type will be guaranteed to be in a certain basic form you can use the various JavaScript String Reference[^] methods to parse the strings and check for patterns etc. For example will all telephone numbers contsain only numbers and specific field delimiters? Will all email addresses contain @ characters etc.

      1 Reply Last reply
      0
      • U User 13942898

        I am calling a third-party API to get customer contact information and it returns a string for each field and my application needs to determine if the string is a phone number or Email or web address

        String data=GetCustomerInfoFromAPI() // will get customer contact information from an API call and will return a string
        

        some examples for the data string I am getting back from the API call
        "www.google.com"
        "602.123.1778"
        "John@gmail.com"

        B Offline
        B Offline
        Blikkies
        wrote on last edited by
        #3

        I'm probably to late but here is it anyway. You can make use of Regular expression (Regex) to validation your strings. Here is an example on how to check if the string is an email address using regex:

        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(String(email).toLowerCase());
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups