473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

adding http:// to website in form

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
7 1594
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Mike | last post by:
I'm used to unix/cgi scripts so im slightly out of my depth here. Ive got an asp script for a website form which works fine. What i want to do is also get the form to include the ip address of the...
4
by: OJ | last post by:
Hi, This works to maximize the window, but wants to load yahoo locally : C:\WINDOWS\Desktop\www.yahoo.com <html> <script type="text/javaScript"> <!-- function test() { qwe =...
3
by: Nik | last post by:
Hi everyone, Is there anyway of letting the user click on a link or image, and saving a shortcut icon to the users desktop? Thanks Nik
1
by: ECathell | last post by:
I am getting an unspecified error when adding a windows form to a project. New project, old project. Doesn't matter. Also happens for user control. All the message box says is Unspecified Error. ...
6
by: Beany | last post by:
i need help!!!!!!! Has any1 got examples of the following that i can view: 1) simple form field Validations 2) Adding to a database using a form with a adding button ??????
2
by: fniles | last post by:
In VB.Net 2003 how can I download a file from an HTTP website ? For example, I need to download file abc.zip from http://mydata.com/mycompany. Thank you.
2
by: Kusank | last post by:
Hi, I don't know anything about php... I was wondering if anyone knew a website or something that i could go to for help on making the form in my website send to the website's email address.
2
by: dan07 | last post by:
Language used: Javascript Application using iFrame: Microsoft CRM 3.0 form Is there a generic way for an iFrame to send a variable to a remote website form? I need to be able to open up the...
1
by: VAIO PC | last post by:
http://dcaic.com/alta_google.htm
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.