Avatar of maccaj51
maccaj51
Flag for Afghanistan asked on

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

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
Regular ExpressionsJavaScriptjQuery

Avatar of undefined
Last Comment
maccaj51

8/22/2022 - Mon
Scott Fell

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.
maccaj51

ASKER
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

Scott Fell

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Scott Fell

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.
maccaj51

ASKER
Yes - correct
ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
maccaj51

ASKER
Thanks Scott
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.