Click to See Complete Forum and Search --> : Please help with script problem
Miahi
11-17-2004, 09:47 PM
Hi...
I have to write a script to validate that the "http://" part of the address is entered into a text box. I need "http://" to appear before the address if it is not entered when enter is pressed. If it is already entered, then I want nothing to happen; for it to stay as it is.
I have this:
<script>
<!--
function Validate(box){
if(box.value.substring(0,7)!=="http://"){
box.value="http://"+box.value
}
}
// -->
</script>
how would I go about making the form do this?
I have this in the <body> section, but it does not work:
<form name="displayform">
<input type="text" id="itemsbox" size="30" onSubmit="Validate(this)">
</form>
If anyone could help me, I would appreciate it a GREAT deal... Thanks!
<script type="text/javascript"><!--
function Validate(box){
if(box.value.substring(0,7)!="http://"){
box.value="http://"+box.value;
}
}
//--></script>
<form name="displayform" action="" method="get" onsubmit="Validate(this.form.itemsbox);">
<fieldset>
<legend>Dummy Form</legend>
<label><input type="text" id="itemsbox" size="30"></label><br>
<label><button type="submit"></label>
</fieldset>
</form>
Miahi
11-17-2004, 10:22 PM
Thanks so much for the help Jona.
I am still having trouble it seems.
When I put in the code you provided... The "http://" didn't appear when enter was pressed. Nothing happened when enter was pressed except it refreshed the page. Maybe I'm placing it wrong... If you have any ideas.. that would be excellent. If not, thanks for the help anyways!!
fredmv
11-17-2004, 10:30 PM
See: <http://www.webdeveloper.com/forum/showthread.php?s=&threadid=49206>.
Miahi
11-17-2004, 10:58 PM
I went to the page you linked and tried using the code you provided, but for some reason, the page is merely refreshed when enter is pressed. I need for "http://" to appear before when not typed in (when enter is pressed). You've been very helpful in responding quickly.. and it may just be my mistake why it isn't working. I know I'm not providing tons of information so I am not asking the easiest question, but any ideas you may have to resolve this problem would be much appreciated. Thanks again.
fredmv
11-17-2004, 11:02 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript">
function fixAddr(el)
{
if(!/^http:/i.test(el.value)) el.value = 'http://' + el.value;
}
</script>
</head>
<body>
<form action="#">
<fieldset>
<legend>Demo</legend>
<label for="addr">Address: <input type="text" id="addr" name="addr" onchange="fixAddr(this)"></label>
<input type="submit" value="Submit">
</fieldset>
</form>
</body>
</html>Although ideally you'd simply present it more like this:<label for="addr">http://<input id="addr" type="text"></label>And then, on the server, prepend "http://" to the string in which they provided. Far more user-friendly, not to mention JavaScript-independent.
Miahi
11-17-2004, 11:17 PM
Thanks for all the help.
The script you provided gave everything I asked.
One question though:
Would it be possible to (when enter would be pressed) have "http://" appear before the address without reloading the page? Where it just appears before, but the page does not reload, and does not erase what's in the text box. Sorry if what I asked makes no sense.
Thanks again for the help! It was extremely useful.
fredmv
11-17-2004, 11:23 PM
No problem.
Give this a shot.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript">
function fixAddr(el)
{
if(!/^http:/i.test(el.value)) el.value = 'http://' + el.value;
return false;
}
</script>
</head>
<body>
<form action="#" onsubmit="return fixAddr(this.elements['addr'])">
<fieldset>
<legend>Demo</legend>
<label for="addr">Address: <input type="text" id="addr" name="addr"></label>
<input type="submit" value="Submit">
</fieldset>
</form>
</body>
</html>
Miahi
11-17-2004, 11:35 PM
Thank you so much. This was exactly what I was looking for. This has really saved me. Thanks!