Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-29
8
Medium Priority
?
432 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 54

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 54

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 54

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 54

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
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…
Suggested Courses

926 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