Replace a character in a string?

Discussion in 'JavaScript' started by Kerosene, Dec 18, 2007.

  1. #1
    I've got a form that redirects a user to a dynamic url based on the word they enter. e.g they enter 'banana' and the form takes them to www.domain.com/banana

    The problem is when somebody enters a word with a dot e.g "banana.apple".
    Then I get a 404.

    So I want to replace '.' with '_dot_' or something BEFORE the form is submitted. How do I do it?

    <form name="openlocation" action="">
    <input type="text" name="href" value="" />
    <input type="button" value="Go!" onClick="location.href=document.openlocation.href.value;" />
    </form>
    Code (markup):

    Or course, if there's a sexy htaccess way to do this, then please tell me :)
     
    Kerosene, Dec 18, 2007 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <form name="openlocation" action="">
    <input type="text" name="href" value="" />
    <input type="button" value="Go!" onClick="location.href=document.openlocation.href.value.replace(/\./, '_dot_');" />
    </form>
    Code (markup):
    maybe something like that.. however i haven't tested it
     
    Jamie18, Dec 18, 2007 IP
    Kerosene likes this.
  3. Kerosene

    Kerosene Alpha & Omega™ Staff

    Messages:
    11,366
    Likes Received:
    575
    Best Answers:
    4
    Trophy Points:
    385
    #3
    Thanks! That works perfectly.

    I should sit down and learn some basic JavaScript one of these days :eek:

    +rep :)
     
    Kerosene, Dec 18, 2007 IP
  4. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    I think that will only replace the first dot, so "apple.pear.banana." would become "apple_dot_pear.banana."

    What you want is:

    value.replace(/\./g, "_dot_");

    to make it "apple_dot_pear_dot_banana_dot_"
     
    lephron, Dec 20, 2007 IP