Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify a URL in a text input 1

Status
Not open for further replies.

bean1234

Technical User
Nov 14, 2006
45
US
Hello Experts,

I have a scenario where I have to indetify the URL with in a text message inserted by the user in a text box, the user enters text which could also contain hyperlinks like I need to identify that and display the url as a URL when the text message is displayed later.

Once solution I can think of is to use a RegExp to parse the text and display it , is there any other possibly simpler way to do it.

Thanks.
 
A regular expression wouldn't really be that complicated, you cuold do most of the work using a replace. Here is an example of detecting URL's using a regex and replacing them with links:
Code:
<html>
<head>
<script language="javascript">
function showPreview(elem){
	var pElem = document.getElementById("prevDiv");
	var content = elem.value;
	pElem.innerHTML = content.replace(/((http|ftp|mailto):\/\/[^\s]+)/gi,"<a href=\"$1\" title=\"external link\">Link</a>");
}
</script>
</head>
<body>
Type:<br/> <textarea onKeyUp="showPreview(this);" cols="40" rows="5"></textarea><br/>
Preview: <div id="prevDiv"></div>
</body>
</html>

That is probably not the best example of a regex to detect URL's, but there are plenty of examples of better ones all over the net.

-T

 
Thanks Trawn,

I will use a Regular Expression for this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top