SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Force http:// at beginning of input field

    Hi Guys!

    What I need to do is force the value "http://" to the beginning of a text input. It shouldn't allow the user to delete it.

    Does anyone know how to do this?

    Thanks in advance.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Zaggs View Post
    What I need to do is force the value "http://" to the beginning of a text input. It shouldn't allow the user to delete it.
    A solution is to not put http:// in the text input.

    For example:

    HTML Code:
    <label>Path: http://<input name="path"></label>
    That way the user gets to visibly see that http:// is already there, and the server when it receives the submitted data can deal with the path as it sees fit. The server can then add http:// to the path.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    A solution is to not put http:// in the text input.

    For example:

    HTML Code:
    <label>Path: http://<input name="path"></label>
    That way the user gets to visibly see that http:// is already there, and the server when it receives the submitted data can deal with the path as it sees fit. The server can then add http:// to the path.
    Hi Paul,

    Thanks for your reply. I already was using this method but unfortunately we cannot depend on the user seeing the "http://". We are getting lots of people still entering http:// and some just entering the domain name (i.e. google.com).

    What I wanted was to pre-populate the text field with "http://" and not let that value be deleted. Is this possible?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Hi Paul,

    Thanks for your reply. I already was using this method but unfortunately we cannot depend on the user seeing the "http://". We are getting lots of people still entering http:// and some just entering the domain name (i.e. google.com).

    What I wanted was to pre-populate the text field with "http://" and not let that value be deleted. Is this possible?
    Not really, no. The problems come in when dealing with the user typing, or pasting an existing URL, because whenever the user edits the value, that changes the position of the insertion bar, and such issues don't tend have cross-browser friendly solutions.

    Validation of user input is a much easier and more effective solution.

    If you want to reinforce to the user what you want the field to start, you can set the default value of the field to http:// to help clue them in to your wishes.

    If after entering their value it doesn't meet up with your requirements, that is where standard validation occurs to resolve such issues.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That is why you validate and filter input before you work with it. If you need a URL with HTTP:// attached then you make sure it is properly formatted when you work with it. You should already be doing this!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •