Thursday, February 17, 2011

How to read text from a textbox and make some of them a link?

I will show the problem with a example.

There is some text in the textbox such here:

Hi! this is a example [lnk]text[/lnk]

When i push the submit button and publish this text, the word in the [lnk] and [/lnk] tags must be a link like this www.mysite.com?link=text.

How do I make it easily with javascript or jquery?

Note: I'm not so good about javascript.

From stackoverflow
  • This will do the javascript for you - not sure if you need to do anything special for asp.net

    <form onsubmit="return doLinks(this.elements['links']);">
    <textarea name="links" rows="20" cols="80"></textarea>
    <input type="submit">
    </form>
    
    <script type="text/javascript">
    
    function doLinks(elm)
    {
        var matches = elm.value.match(/\[link\](.*?)\[\/link\]/gi);
        for (var i = 0; i < matches.length; i++)
        {
         var url = 'http://www.mysite.com/?link=' + encodeURIComponent(matches[i].substring(6, matches[i].length - 7));
         elm.value = elm.value.replace(matches[i], url);
        }
    
        return true;
    }
    
    </script>
    

0 comments:

Post a Comment