SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic form content: remove http://

    I have a text field for a user to enter a URL. When I call the form data with PHP I'm adding the http:// into the link like this: <a href="http://' . $url . '">

    I'm wondering if javascript can help me check to see if the user has entered the http:// and if so, remove it.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Code Javascript:
    if (document.getElementById("some_input_field").value.substring(0,7) == "http")
    {
      document.getElementById("some_input_field").value = document.getElementById("some_input_field").value.substring(7);
    }

    Where "some_input_field" is the id of the input field.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code idea ScallioXTX. I'm pretty new to working JS into my page like this. Would you mind explaining the code a bit?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Oops, just saw I made an error ...
    It's supposed to be

    Code javascript:
    if (document.getElementById("some_input_field").value.substring(0,7) == "http://")
    {
      document.getElementById("some_input_field").value = document.getElementById("some_input_field").value.substring(7);
    }

    Basically it checks if the first 7 characters form the character sequence http:// and if does, remove the first 7 characters from the input field. The first 7 characters are of course http:// so you remove that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •