Solved

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

Posted on 2016-09-29
8
32 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
  • 4
  • 3
8 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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
Comment Utility
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 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
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
Comment Utility
Yes - correct
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
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
Comment Utility
Thanks Scott
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now