473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Changing a string into a Hyperlink

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
7 2349
acoder
16,027 Expert Mod 8TB
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
JR123
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
16,027 Expert Mod 8TB
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
JR123
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
16,027 Expert Mod 8TB
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
JR123
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
16,027 Expert Mod 8TB
Post the whole code then and I'll test it.
Feb 9 '07 #8

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

Similar topics

8
by: Rob | last post by:
Hi all, Is it possible to change the Session.LCID in a hyperlink? My problem is I'm calling a Date from a database to use as a querystring in the hyperlink but I also need to display the date as...
4
by: Andreas Meffert | last post by:
Hello, How can I change the Type of a field in a table from Memo to hyperlink? I import a table from Oracle to Access 2003. After that, the field type of some hyperlink-fields is "Memo". How...
5
by: Martha | last post by:
When I move my mouse over a hyperlink component, the hyperlink does not change color. How do I change the color of a hyperlink when the mouse goes over the hyperlink? or Change the color of a...
1
by: Paul | last post by:
There are a number of places in my app where I want the user to work with an item (a real physical item referred to by its stock number) If such an item hasn't been selected, I redirect the user...
1
by: Nathan Sokalski | last post by:
I am using the ImageUrl property of the Hyperlink control to create a graphical Hyperlink. However, I want to change the size of the image I am using, but the generated HTML places the width/height...
3
by: Janelle.Dunlap | last post by:
I have an Excel spreadsheet containing a column of hyperlinks that I need to import into Access. I have no trouble importing (in my case actually linking) the spreadhsheet to the Access table,...
1
by: sanju | last post by:
Hi , I am displaying 5 photos of a user(getting image path from database) and binding them to hyperlink control and adding that Hyperlink control to Datalist container in code behind file. ...
1
by: Frank Milverckowitz | last post by:
Hi, I'm trying to do something common and what should be simple using the GridView "Add Columns" feature in Visual Studio 2005. All I want to do is add a Hyperlink column that will take the...
0
by: =?Utf-8?B?R2FyeSBMYXJpbWVy?= | last post by:
On an aspx page I have text that is a Hyperlink to an html page. The text is not on a button, but is just plain text that has been converted to a Hyperlink. Is it possible to specify or change...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.