Solved

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

Posted on 2016-09-29
8
161 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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 500 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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 code hide show and toggles. 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…
Suggested Courses

634 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