Click to See Complete Forum and Search --> : Check URL field content in form


bobafifi
03-22-2003, 08:41 AM
I need a JavaScript that will check that on submit, the first seven characters of the URL field in my form http://www.usedflutes.com/addpage.html equals the default value http:// or an alert will be prompted. Anybody have a script that can do this?

Many thanks in advance,

-Bob

requestcode
03-22-2003, 08:55 AM
You could have a script like this using the substr to check the field:
<script language="JavaScript">
function checkURL(frmobj)
{
if(frmobj.myurl.value.substr(0,6)!="//http")
{
alert("Invalid URL!")
frmobj.myurl.focus()
return false
}
}
</script>

Then you would perform this by using the onSubmit event in the form tag like this:
<form name="myform" method="post" onSubmit="return checkURL(this)">

The "return false" in the script keeps the form from being submitted if the value is incorrect. The substr method checks the value of the text box "myurl" starting in the first position and up to and including the 6th position.

bobafifi
03-22-2003, 09:49 AM
Thanks for the help! :-) Unfortunately, I'm not having much luck getting your script to work...
If it's not too much of a hassle, can you please show me how to install it so that it'll work with the other JavaScrpts already there ( http://www.usedflutes.com/addpage.html )?

Thanks again,

-Bob

bobafifi
03-22-2003, 08:29 PM
what does "frmobj" mean?? I'm guessing "form object" - is that right? If so, what is it's function?

Thanks again,

-Bob

bobafifi
03-22-2003, 08:49 PM
Wow! Thanks Dave :-)
I'll see if I can get it to work right now.

Thanks again,

-Bob

bobafifi
03-22-2003, 09:38 PM
Hi Dave,
Well I think I might be in "over my head"...
I'm not getting it to work with the existing stuff in my form...
Here's the form (less your code):

<P><FONT SIZE="-1" FACE="Verdana"><TABLE BORDER=0 BGCOLOR="#9BBAD6">
<TR>
<TD>
<P><FORM ACTION="/cgi-bin/guestbook27.pl" METHOD=POST name=myForm onsubmit="return validatePwd()">
<P><B>Title </b> (headline)<B>:</b>
<INPUT TYPE=text NAME=realname VALUE="" SIZE=39 MAXLENGTH=45><BR>
<B> E-mail:</b> <INPUT TYPE=text NAME=username VALUE="" SIZE=40><BR>
<B> Re-type E-mail:</b>
<INPUT TYPE=text NAME=control VALUE="" SIZE=40><BR>
<B> URL</b> (photos, Web site)<B>:</b> </font><INPUT TYPE=text NAME=url VALUE="http://" SIZE=40><BR>
<B> City:</b> <INPUT TYPE=text NAME=city VALUE="" SIZE=15>,
<B> State:</b> <INPUT TYPE=text NAME=state VALUE="" SIZE=2>
<B> Country:</b> <INPUT TYPE=text NAME=country VALUE="USA " SIZE=15></p>

<P><B>Description:</b><BR>
<TEXTAREA NAME=comments ROWS=4 COLS=82 WRAP=virtual onKeyPress="checkCapsLock( event )" onkeydown="textCounter(document.myForm.comments,document.myForm.remLen2,640)" onkeyup="textCounter(document.myForm.comments,document.myForm.remLen2,640)"></textarea><BR>
<INPUT TYPE=text NAME=remLen2 VALUE="640" SIZE=3 MAXLENGTH=3 readonly>
<FONT SIZE="-1">characters max.</font></p>

<P>Click <B>once</b> to
<INPUT TYPE=submit NAME=Submit VALUE="Submit Ad"> or
<INPUT TYPE=reset VALUE="Clear Form"> to start over
</form></p>
</td>
</tr>
</table>
</font></p>

=====
Do I stick your code in the form or above it?

Thanks again for your help Dave,

-Bob

bobafifi
03-22-2003, 10:16 PM
Hi Dave,
I just got another "spam-ad" (I get several every week from India, Pakistan and Nepal) which having nothing to do with the site. The title is almost always "START EARNING TODAY."
Would love to have a JavaScript which would disable the "Submit" button if the Title Field equals "START EARNING TODAY" - do you have something that'll do that??

Many thanks in advance Dave,

-Bob

bobafifi
03-22-2003, 11:16 PM
Hi Dave,
hmmm...I tried to put the code in as you suggested but the alert did not activate when I tested it and disabled the other JavaScripts. Not sure what's up...
Could you please take a look at my install and see??
Test page is at http://www.usedflutes.com/addpage_test.html

Thanks again Dave,

-Bob

Nedals
03-23-2003, 01:31 AM
Here's a reatively simple method, also using a regular expession.

var data = "http://www.domain.com";
if (/^http:\/\//.test(data)) { alert('ok') } else { alert('no good') }

bobafifi
03-23-2003, 01:40 AM
Hi Nedals,
Thanks, but how does does this work?? http://www.usedflutes.com/addpage_test.html activates the "ok" alert when page is called and that's it...
Is there some way to fix it?

Thanks anyway

-Bob

Nedals
03-23-2003, 01:58 AM
Are you familiar with regular expressions? They are fun :)

Let me briefly re-write my solution...
var re = /^http:\/\//; // the regular expression
if (re.test(data)) { alert('ok') } else { alert('no good') }

/ ^ http: \/\/ / ... is a regular expression

/ ... is the char used for the start and end of the experssion
^ ... marks the beginning of the string to check
http:// ... is the string to check BUT // needs to be escaped (ie use the literal char) so as not to be confused with the start/end chars. The backslash is used to do that. Hence \/\/.

Is that what you wanted?
Regular expressiona are extremely powerful and well worth studying. They are user extensively in Perl.

Nedals
03-23-2003, 02:07 AM
We posted at the same time and you added to your post. I also got a look at your code and see you need a little more help than my post will povide. Do me a favor, please put all that javascript into the <head> fo your doc. I'll take a look at it tomorrow.

bobafifi
03-23-2003, 08:47 AM
Good Morning Dave,
Sorry to have lost your help on this...
It's obvious you know what you're doing and your 'fix' looks right.
I did try several times to integrate it into my page but kept losing all the JavaScript functionality ( see http://usedflutes.com/addpage_Dave_Clark.html ).
I do very much appreciate your help Dave with this and the time you've already spent on it.

Thanks again,

-Bob

bobafifi
03-23-2003, 09:52 AM
Hi Dave,
You mean like this??

http://usedflutes.com/addpage_Dave_Clark.html

If so, I must be missing something because the other JavaScripts are now disabled...

Thanks once again Dave for help with this,

-Bob

bobafifi
03-23-2003, 10:18 AM
Thanks Dave!
It's working great! :-)

-Bob

Nedals
03-23-2003, 11:04 AM
As promised, I took a look at your code and incorporated my solution. I was happy to see Dave got you up and running. I've attached my code for you to see another approach.