By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

adding http:// to website in form

P: n/a
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would become http://domain.com
but http://example.com would be left alone.

tia,
Chris
Jan 20 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Chris wrote on 20 jan 2008 in comp.lang.javascript:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would become http://domain.com
but http://example.com would be left alone.
Yes.

Use Javascript.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '08 #2

P: n/a
Chris said the following on 1/20/2008 2:06 PM:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would become http://domain.com
but http://example.com would be left alone.
If the data is being submitted to the server, then you need to do it on
the server if you want it to be reliable.

Client-side, it could be as simple as reading the first 7 characters of
the value of the input. There are other ways as well using Regular
Expressions and even indexOf.

var firstSeven = fieldRef.substring(0,7)
if (firstSeven != "http://"){
fieldRef.value = "http://" + fieldRef.value
}

One thing you didn't answer is what happens if the value starts with
https://, ftp:// or any other valid protocol?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 20 '08 #3

P: n/a
VK
On Jan 20, 10:06 pm, Chris <chris95...@yahoo.comwrote:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would becomehttp://domain.com
buthttp://example.comwould be left alone.
If you have a guarantee that all resources will be for http protocol
exclusively and not for http or https or ftp or news etc. Otherwise
this helper becomes a great annoyance.

<input type="text" name="URL" onblur="
if (this.value.indexOf('http://') != 0) {
this.value = 'http://' + this.value;
}">

but really what you _should_ do is to simply pre-fill the input with
http://
<input type="text" name="URL" value="http://">

If visitor has http resource to share then she will appreciate your
care. If it's another type of resource then user will simply delete
the helper and type/paste whatever is really needed.
Jan 20 '08 #4

P: n/a
VK
On Jan 20, 10:27 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Chris wrote:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would becomehttp://domain.com
buthttp://example.comwould be left alone.

Yes, there is.
Net result of this thread: two a** h***s against two men :-)
Jan 20 '08 #5

P: n/a
On 20 , 21:37, VK <schools_r...@yahoo.comwrote:
On Jan 20, 10:27 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Chris wrote:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. i.e. domain.com would becomehttp://domain.com
buthttp://example.comwouldbe left alone.
Yes, there is.

Net result of this thread: two a** h***s against two men :-)
Solution with regular expression:
function formatValue(aString)
{
return aString.replace(/^(?:(?!http:\/\/).)+/i,"http://"+aString);
}
Jan 21 '08 #6

P: n/a
Georgi Naumov wrote on 21 jan 2008 in comp.lang.javascript:
On 20 , 21:37, VK <schools_r...@yahoo.comwrote:
>On Jan 20, 10:27 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Chris wrote:
Is there a way when a user fills out a form and it asks them to
enter a website that it'll be reformatted with http:// in front
of it if it isn't already there. i.e. domain.com would
becomehttp://domain.com buthttp://example.comwouldbe left alone.
Yes, there is.

Net result of this thread: two a** h***s against two men :-)
???
Solution with regular expression:
function formatValue(aString)
{
return
aString.replace(/^(?:(?!http:\/\/).)+/i,"http://"+a
String);
}
Shorter and more comptible with older versions:

function formatValue(aString) {
return aString.replace(/^(http:\/\/)?/i,"http://");
};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 21 '08 #7

P: n/a
On Jan 20, 2:34*pm, VK <schools_r...@yahoo.comwrote:
On Jan 20, 10:06 pm, Chris <chris95...@yahoo.comwrote:
Is there a way when a user fills out a form and it asks them to enter
a website that it'll be reformatted with http:// in front of it if it
isn't already there. *i.e. domain.com would becomehttp://domain.com
buthttp://example.comwouldbe left alone.

If you have a guarantee that all resources will be for http protocol
exclusively and not for http or https or ftp or news etc. Otherwise
this helper becomes a great annoyance.
Well put.
>
<input type="text" name="URL" onblur="
*if (this.value.indexOf('http://') != 0) {
* this.value = 'http://' + this.value;
*}">
This is silly. Tabbing through an empty field will populate it with
"http://". What if the user wanted to leave it blank? What if they
enter "No protocol" and hit enter? Or perhaps there is a leading
space.

If the OP has no way to validate on the server (which would be
unfortunate as you cannot rely on script), then the only proper
solution is to validate the input on submit (and possibly add the
protocol without bothering the user.) If the OP can validate on the
server, then the server can add the protocol at that point.

Jan 21 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.