View Full Version : Replace "http://" with ""(blank,nothing,etc)
Deanimal
10-24-2002, 09:30 PM
How do you replace one value with another? If the user tabs past the url field in my form, leaving the "http://", I want to replace it with a blank entry. I tried using double quotes (""), null, and undefined, and it doesn't work yet. Here's how I wrote it:
if (s_url == "http://")
{
s_url = "";
return true;
}
Thanks,
ConfusedOfLife
10-24-2002, 09:59 PM
<form onsubmit="this.elements[0].value = this.elements[0].value.replace(/http:\/\//, '');">
<input type="Text">
<input type="submit">
</form>
joh6nn
10-24-2002, 10:48 PM
can we see the rest of the code you're using? using empty quotes like that ( "" ), is right, so if it's not working, then there's something else wrong.
Deanimal
10-25-2002, 01:02 AM
Sure, here it is. I've trimmed it down for posting but I think the essentials are there:
function IsItPresent(s,explanation)
{
// developed by www.willmaster.com
s = StripSpacesFromEnds(s);
if(s.length) return s;
alert('Please enter ' + explanation + '.');
return;
}
function CheckURL(s_url)
{
if(! s_url) return true;
var i = s_url.indexOf(' ',0);
while(i> -1)
{
s_url = s_url.substring(0,i) +
s_url.substring((i + 1),s_url.length);
i = s_url.indexOf(' ',0);
}
document.form1.url.value = s_url;
// below 6 lines are for those who tab over the url field
// and leave the http://
// figure out how to change it to blank.
if (s_url == "http://")
{
s_url = "";
return true;
}
else
{
if((s_url.indexOf('http://',0) != 0) ||
(s_url.length <11) ||
(s_url.indexOf('.',0) <8) ||
(s_url.lastIndexOf('.')> (s_url.length - 3)) ||
(s_url.lastIndexOf('.') <(s_url.length - 4)))
{
alert('The URL ' + s_url + ' is not valid.');
return false;
}
}
return true;
}
Deanimal
10-25-2002, 01:06 AM
Sorry for the formatting. What should I do, put it in as "code"? I don't see that option here. The tabs were there before it was posted.
whammy
10-25-2002, 01:10 AM
To preserve the formatting, use this when you post:
<!-- put the code here -->
:)
Deanimal
10-25-2002, 01:18 AM
Thanks whammy, that makes sense.
whammy
10-25-2002, 01:23 AM
P.S. You can edit your post and do it too... :D
And here's the link you should read:
http://www.codingforums.com/misc.php?s=&action=bbcode
And you're welcome. :)
joh6nn
10-25-2002, 02:17 AM
you're gonna kick yourself on this:
document.form1.url.value = s_url;
// below 6 lines are for those who tab over the url field
// and leave the http://
// figure out how to change it to blank.
if (s_url == "http://")
you assign the text back to the form field, then you check to see if it was left empty. you never assign the text to the form field after you've checked if it was empty.
Deanimal
10-25-2002, 03:47 AM
Would something like this work?
function CheckURL(s_url)
{
if (s_url == "http://")
{
s_url = "";
}
if(! s_url) return true;
var i = s_url.indexOf(' ',0);
while(i> -1)
{
s_url = s_url.substring(0,i) +
s_url.substring((i + 1),s_url.length);
i = s_url.indexOf(' ',0);
}
document.form1.url.value = s_url;
etc etc etc etc etc etc
(This is pretty new to me)
joh6nn
10-25-2002, 05:00 AM
this should do it:
function CheckURL(s_url)
{
if(! s_url) return true;
var i = s_url.indexOf(' ',0);
while(i> -1)
{
s_url = s_url.substring(0,i) +
s_url.substring((i + 1),s_url.length);
i = s_url.indexOf(' ',0);
}
// below 6 lines are for those who tab over the url field
// and leave the http://
// figure out how to change it to blank.
if (s_url == "http://")
{
s_url = "";
}
else
{
if((s_url.indexOf('http://',0) != 0) ||
(s_url.length <11) ||
(s_url.indexOf('.',0) <8) ||
(s_url.lastIndexOf('.')> (s_url.length - 3)) ||
(s_url.lastIndexOf('.') <(s_url.length - 4)))
{
alert('The URL ' + s_url + ' is not valid.');
return false;
}
}
document.form1.url.value = s_url;
return true;
}
Deanimal
10-25-2002, 10:01 AM
joh6nn,
Thanks for helping. I'm completely new to programming and realize what I really need to do is study some tutorials from the beginning. That would be better. But I have a guestbook that's almost ready to add to my site, and the only thing left is to get the validation a little better. So I'm trying to do quite a lot more than I know. Thanks for giving me something to work with, and I'll let you know how it does.
ps: just to clarify, my form has "http://" already entered. If the user leaves it like that with no entry, it needs to change to blank so that it doesn't print in the guestbook as a link like this- http://. In addition, if they delete it, then a completely blank field needs to validate too:
http:// ========= change to nothing, and validate as true
"" ========= validates as true
anything else ==== check for correct url format
I'll work on it in a few hours.
Talk with you later,
Deanimal
10-25-2002, 10:42 AM
I lied. Couldn't wait a few hours so I just did it and guess what? It works! Cool; neato; wicked awesome.
As I put in the changes it started making sense; and I see why it wasn't working before, and why it works now. My eyes are starting to open [:)] .
Thanks so much for helping. It may have seemed simple and remedial to you, but for me it's all completely new.
marslee
10-26-2002, 07:37 AM
Originally posted by ConfusedOfLife
<form onsubmit="this.elements[0].value = this.elements[0].value.replace(/http:\/\//, '');">
<input type="Text">
<input type="submit">
</form>
why it doesn't work?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.