By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Changing a string into a Hyperlink

P: 5
I need some help. When the user enters a number if there is a match I want it to display as a hyperlink to that specific section of an intranet site we have at work. I have quite a lot of data but have only put a few entries on as a example and not sure how to get it to display as a link.

[HTML]<html>
<head>
<TITLE>number Search</TITLE>
<script type="text/javascript">
var numbers = {"^[A-Z]{3}[A-Z0-9]$" : "Google",
"^S[A-Z0-9]{3}$" : "Yahoo",
"^G[A-Z0-9]{3}$" : "Hotmail",
"^U291$" : "Google"};

function getPhoneNumber(theForm)
{
var numberString = theForm.number.value;
var phoneNumField = theForm.phoneNum;
var foundPhoneNum = false;

var currRegExp;
for (var pattern in numbers)
{
currRegExp = new RegExp(pattern, "i");
if (currRegExp.test(numberString))
{
phoneNumField.value = numbers[pattern];
foundPhoneNum = true;
break;
}
}

if (!foundPhoneNum)
alert("no match");
}
</script>
</head>

<body>
<form>

<label for="number">Please Enter Number </label><input type="text" name="number" id="number">
<input type="button" value="Find" onclick="getPhoneNumber(this.form)">

<label for="phoneNum">The webpage is </label><input type="text" name="phoneNum" id="phoneNum">
<input TYPE="reset" NAME="Clear" VALUE="Clear">
</form>
</body>
</html>[/HTML]
Feb 7 '07 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Try the following:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var numbers = {"^[A-Z]{3}[A-Z0-9]$","^S[A-Z0-9]{3}$","^G[A-Z0-9]{3}$","^U291$"};
  3. var links = {"www.google.com","www.yahoo.com","www.hotmail.com","www.google.com"};
  4. function getPhoneNumber(theForm)
  5. {
  6.   var numberString = theForm.number.value;
  7.   var phoneNumField = theForm.phoneNum;
  8.   var foundPhoneNum = false;
  9.  
  10.   var currRegExp;
  11.   var pattern;
  12.   for (i = 0; i < numbers.length; i++)
  13.   {
  14.     pattern = numbers[i];
  15.     currRegExp = new RegExp(pattern, "i");
  16.     if (currRegExp.test(numberString))
  17.     {
  18.       phoneNumField.value = links[i];
  19.       foundPhoneNum = true;
  20.       break;
  21.     }
  22.   }
  23.  
  24.   if (!foundPhoneNum)
  25.     alert("no match");
  26. }
  27. </script>
  28.  
Note I have created two arrays instead of one. Use the second one to store the links. If you still have problems, post again.
Feb 7 '07 #2

P: 5
Try the following:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var numbers = {"^[A-Z]{3}[A-Z0-9]$","^S[A-Z0-9]{3}$","^G[A-Z0-9]{3}$","^U291$"};
  3. var links = {"www.google.com","www.yahoo.com","www.hotmail.com","www.google.com"};
  4. function getPhoneNumber(theForm)
  5. {
  6.   var numberString = theForm.number.value;
  7.   var phoneNumField = theForm.phoneNum;
  8.   var foundPhoneNum = false;
  9.  
  10.   var currRegExp;
  11.   var pattern;
  12.   for (i = 0; i < numbers.length; i++)
  13.   {
  14.     pattern = numbers[i];
  15.     currRegExp = new RegExp(pattern, "i");
  16.     if (currRegExp.test(numberString))
  17.     {
  18.       phoneNumField.value = links[i];
  19.       foundPhoneNum = true;
  20.       break;
  21.     }
  22.   }
  23.  
  24.   if (!foundPhoneNum)
  25.     alert("no match");
  26. }
  27. </script>
  28.  
Note I have created two arrays instead of one. Use the second one to store the links. If you still have problems, post again.
Hi
I tried your code as above but cant seem to get it working, not sure what ive done wrong?
Feb 8 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Ok, I misunderstood your problem. Take your original code which was working fine. Now instead of displaying in a text box, use a div and display on that instead:
Expand|Select|Wrap|Line Numbers
  1. phoneNumLink.innerHTML = "<a href='"+numbers[pattern]+"'>"+numbers[pattern]+"</a>";
where phoneNumLink is a div object. AFAIK, you can't display links in text boxes.
Feb 8 '07 #4

P: 5
Hi
I tried this out and got rid of the text box but I still can t seem to get this working, its probably something stupid.
Feb 9 '07 #5

acoder
Expert Mod 15k+
P: 16,027
Try this modified code:
Expand|Select|Wrap|Line Numbers
  1. var numbers = {"^[A-Z]{3}[A-Z0-9]$"     : "http://www.google.com",
  2.         "^S[A-Z0-9]{3}$"         : "http://www.yahoo.com",
  3.         "^G[A-Z0-9]{3}$"         : "http://www.hotmail.com",
  4.         "^U291$"                 : "http://www.google.com"};
  5.  
  6. function getPhoneNumber(theForm)
  7. {
  8.   var numberString = theForm.number.value;
  9.   var phoneNumLink = document.getElementById("phoneNum");
  10.   var foundPhoneNum = false;
  11.  
  12.   var currRegExp;
  13.   for (var pattern in numbers)
  14.   {
  15.     currRegExp = new RegExp(pattern, "i");
  16.     if (currRegExp.test(numberString))
  17.     {
  18.       phoneNumLink.innerHTML = "<a href='"+numbers[pattern]+"'>"+numbers[pattern]+"</a>";
  19.       foundPhoneNum = true;
  20.       break;
  21.     }
  22.   }
  23.  
  24.   if (!foundPhoneNum)
  25.     alert("no match");
  26. }
and in the HTML
[HTML]The webpage is <div name="phoneNum" id="phoneNum"></div>[/HTML]
Feb 9 '07 #6

P: 5
Try this modified code:
Expand|Select|Wrap|Line Numbers
  1. var numbers = {"^[A-Z]{3}[A-Z0-9]$"     : "http://www.google.com",
  2.         "^S[A-Z0-9]{3}$"         : "http://www.yahoo.com",
  3.         "^G[A-Z0-9]{3}$"         : "http://www.hotmail.com",
  4.         "^U291$"                 : "http://www.google.com"};
  5.  
  6. function getPhoneNumber(theForm)
  7. {
  8.   var numberString = theForm.number.value;
  9.   var phoneNumLink = document.getElementById("phoneNum");
  10.   var foundPhoneNum = false;
  11.  
  12.   var currRegExp;
  13.   for (var pattern in numbers)
  14.   {
  15.     currRegExp = new RegExp(pattern, "i");
  16.     if (currRegExp.test(numberString))
  17.     {
  18.       phoneNumLink.innerHTML = "<a href='"+numbers[pattern]+"'>"+numbers[pattern]+"</a>";
  19.       foundPhoneNum = true;
  20.       break;
  21.     }
  22.   }
  23.  
  24.   if (!foundPhoneNum)
  25.     alert("no match");
  26. }
and in the HTML
[HTML]The webpage is <div name="phoneNum" id="phoneNum"></div>[/HTML]
Hi
I tried the code but still cant get a link to display?
Feb 9 '07 #7

acoder
Expert Mod 15k+
P: 16,027
Post the whole code then and I'll test it.
Feb 9 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.