Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jquery/JS : How to detect a telephone number or email within a string?

Posted on 2016-09-29
8
Medium Priority
?
304 Views
Last Modified: 2016-09-29
Hi Experts,

I am grabbing the value of a textarea on blur and want to see whether it has an email address (xxxx@xxx.xxx) and/or a telephone number (0123456780 or 01234 567890)

Any help would be really well received
Thanks
0
Comment
Question by:maccaj51
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41822068
How much validation do you need?  Can you just look to see if there is an @ sign for instance and if that is the case, call it an email else call it a phone number?  Or do you need more detailed algo for email and phone as in it could be neither (ie 5 digit number).

The simple example may be
var string = "somebody@example.com";
var isEmail = string.includes("@");
if(isEmail){
  alert("email");
// treat as email
} else {
  alert("phone");
 // treat as phone
}

Open in new window


You may want to do additional validation though.
0
 

Author Comment

by:maccaj51
ID: 41822101
Hi There,

Thanks for answering so quick. I need to detect if both are present - not either or.

I've found this for emails which seems half decent as a starting point

function extractEmails (text)
{
    return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}

Open in new window

0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41822197
There is one text field to test, right?

http://jsbin.com/nukaterugi/edit?html,output
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input id="test" placeholder="start here">
  <input placeholder="tab to here">
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
  <script>
    $(function(){
  $('#test').on('blur',function(){
     var testValue = $('#test').val();
    
     alert(detect(testValue));
  });
});

function detect(str){
  if(str.includes("@")){
    return "email";
  } else {
    return "phone";
  }
}


  </script>

</body>
</html>

Open in new window


I am not well versed in regular expressions but you can play with that as well.  The logic will still be similar.  There are going to be multiple ways to get to the same thing here.
0
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 41822209
I'm sorry, I didn't read your question correctly.  You are saying there could be both and you need to extract both if available.
0
 

Author Comment

by:maccaj51
ID: 41822213
Yes - correct
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 41822250
Like I said, I am not great with regex and I would suggest a new question just dealing with each of the regex portions. But the idea would be something like this.

http://jsbin.com/mozericite/edit?html,output
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input id="test" placeholder="start here">
  <input placeholder="tab to here">
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
  <script>


$(function(){
  $('#test').on('blur',function(){
     var str = $('#test').val();
     
     var email = extractEmails(str);
     var phone = extractPhone(str);
   
     alert(email);
     alert(phone);
  });
});

function extractEmails (text)
{
    return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}

function extractPhone (text)
{
  
  return text.match( /\d+/);
}



  </script>

</body>
</html>

Open in new window


note that asd@example.com 1234 works but asd@example.com1234 is a false positive for email.
0
 

Author Closing Comment

by:maccaj51
ID: 41822423
Thanks Scott
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question