Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onsubmit error - client side - what's wrong with this code? 3

Status
Not open for further replies.

scroce

MIS
Nov 30, 2000
780
US
All I'm trying to do here is to verify that a field on a HTML form is filled in before it is sent to the server before processing.

Here is the code I have come up with:

<script language=&quot;VBScript&quot;>
Function QuickQuote_onsubmit()
If Len(document.QuickQuote.School Name) <1 then
msgbox (&quot;you gotta enter a school name&quot;)
QuickQuote_onsubmit = false
End IF
End Function
</script>

It keeps giving me a run time error saying &quot;expected )&quot;

The name of the form is &quot;QuickQuote&quot; which is on a document called quickquote.html

here is my form tag:

<form action=&quot;QuickQuoteProc.asp&quot; method=&quot;POST&quot; name=&quot;QuickQuote&quot;>

the name of the field is &quot;School Name&quot;

I hope I gave enough info.

Can somebody point me in the right direction here.

Thank you.

SC.
 
Hi,

Why not put in a little Javascript to test it on the client-side before the form is submitted. You can then halt the submit and have the form entered correctly and resubmitted without the error.

Bastien
 
I see 2 places that might give you errors
1) If Len(document.QuickQuote.School Name) <1 then
The space between School and name. Rename the field, such as School_Name or SchoolName

2) msgbox (&quot;you gotta enter a school name&quot;)
should read msgbox &quot;you gotta enter a school name&quot;



 
write javascript function like
function emp()
{
if(document.formname.fieldvalue==&quot;&quot;)
alert (&quot;enter the value&quot;)
}

call this function on onsubmit of form

<form name=form1 method=post action=somepage.asp Onsubmit=&quot;emp()&quot;>


this will solve ur problem


 
Thanks for everyone who posted.

I had already tried renaming the field from school name to
schoolname, but that didn't work.

TimLarkin wrote:

2) msgbox (&quot;you gotta enter a school name&quot;)
should read msgbox &quot;you gotta enter a school name&quot;

I was taking this example directly from MSDN, and the one that they showed used parens (i'm not looking at it right now, but i'm almost positive it did - why would they put incorrect syntax in their help pages???????) - so I will double check this and post back w/results

and to farhanakhan and bastien:
yes, My next step, in fact, was to find some javascript to do it, however, I have not had a chance to really delve into the workings of javascript yet - (i am still a novice jedi) HOWEVER, I was under the assumption you could do the same thing using VBScript and have been perplexed why the code hasn't worked. I hate it when something that should work doesn't.

I will attempt all of your suggestions and post back soon.

Again, thank you all.

 
it didn't work

I'm sure I'm doing something wrong that's simple that I'm not seeing.

I tried both vb suggestions and I tried the java script. It's as if the browser does not even see the script at all, it may as well not even be there. I mean it should pop up with the message box before any info gets passed to the server right? Well nothing doing, the info from the form gets processed right away.

Maybe it's the actual location of the script on the form page? could I have it in the wrong location?

anybody have any further suggestions?

I've gotten the message box to pop up on its own unrelated to anything else, but the minute I try to add a condition to it, it doesn't work at all.

 
Scroce,

I had the same error and this line was the source. I then trimed the space in &quot;SchoolName&quot; and no &quot;)&quot; error, but it never fired the message when the len was . Then I added the &quot;.value&quot; and everything is fine!!!

Working Code:
<script language=&quot;VBScript&quot;>
Function QuickQuote_onsubmit()
If Len(document.QuickQuote.SchoolName.value) < 1 then
msgbox (&quot;you gotta enter a school name&quot;)
QuickQuote_onsubmit = false
End IF
End Function
</script>

<form action=&quot;QuickQuoteProc.asp&quot; method=&quot;POST&quot; name=&quot;QuickQuote&quot;>
<INPUT type=&quot;submit&quot; value=&quot;Submit&quot; id=submit1 name=submit1>
<INPUT type=&quot;text&quot; id=SchoolName name=SchoolName>
</form> hope this helps!
smbure
 
Scroce,

Sorry - last message was cryptic so:

I had the same error and this line was the source &quot;If Len(document.QuickQuote.School Name) <1 then&quot;. I then trimed the space in &quot;SchoolName&quot; and no &quot;)&quot; error, but it never fired the message when the len was 0. Then I added the &quot;.value&quot; and everything is fine!!!

Working Code:
<script language=&quot;VBScript&quot;>
Function QuickQuote_onsubmit()
If Len(document.QuickQuote.SchoolName.value) < 1 then
msgbox (&quot;you gotta enter a school name&quot;)
QuickQuote_onsubmit = false
End IF
End Function
</script>

<form action=&quot;QuickQuoteProc.asp&quot; method=&quot;POST&quot; name=&quot;QuickQuote&quot;>
<INPUT type=&quot;submit&quot; value=&quot;Submit&quot; id=submit1 name=submit1>
<INPUT type=&quot;text&quot; id=SchoolName name=SchoolName>
</form>
hope this helps!
smbure
hope this helps!
smbure
 
Use javascript, and a return value -- like so:

<form action=&quot;QuickQuoteProc.asp&quot; method=&quot;POST&quot; name=&quot;QuickQuote&quot; onSubmit=&quot;return checkMe();&quot;>

function checkMe(){
if (document.QuickQuote.SchoolName.value == ''){
alert ('You must enter a school name!');
return false;
}
return true;
}

So, if it passes the test, and the field is not blank, it will first show the alert, and then once the user clicks ok, it will return false to the form tag, thereby assuring that the form will not be submitted.

hope it helps! :)
Paul Prewett
 
smbure,

Well, that little &quot;.value&quot; thing was the ticket to the VBScript issue. I'm incredulous. I mean, I modeled my code after an example directly from MSDN, and it didn't show the use of the .value. sheesh.

link9

thanks for the tips on the javascript. This will help me to start to get on track with it.

 
heh -- Just read my post up there... kinda had it backwards -- the explanation that is... It should say that if it FAILS the test, and the field is empty... etc...

Glad it helped! :)
paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top