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!

How to stop from submitting a form 1

Status
Not open for further replies.

ganusha

Programmer
May 1, 2002
28
US
Hi
I'm using vbscript to Validate a form.
i first check if the text box contains anything and if not i pop up a msg and i want the focus to remain on that textbox and not submit the form.

<script language=vbscript>
sub check()

if document.find.text1.value=&quot;&quot; then
msgbox &quot;Enter some name to find the
corresponding address.&quot;
(what should i write here)

else
document.find.submit

end if
end sub
</script>
<form name=&quot;find&quot; action=&quot;procs.asp&quot; method=post>
<P><INPUT id=text1 name=txtName>&nbsp; </P>
<P><INPUT id=submit1 name=submit1 type=submit value=Find onclick=&quot;check()&quot;></P>

</form>
 
although I recommend doing these kind of conditions via javascript here is how you would perform this in vbscript

first thing you need to do is take the onClick out and create a function instead of a sub like this
Function find_OnSubmit()

then you need to give the form a true false value onSubmiting it like this so we can stop it when a condition is met
find_OnSubmit = False

then to make like easier Set I like to declare a var for document.formname like this
Set form = Document.find
this just makes things easier to deal with and read when the form gets exstensive. Good coding practice!!

OK now you have the basic's and can run your condition and if met set focus to the field and set the form onSubmit to false stopping the submission process
here's a complete example based on your form
<html>
<head>
<script language=vbscript>
Function find_OnSubmit()
find_OnSubmit = False
Set form = Document.find
if form.txtName.value = &quot;&quot; then
msgbox &quot;Enter some name to find the corresponding address.&quot;
form.text1.focus
else
find_OnSubmit = True
End If
End Function
</script>
</head>
<body>
<form name=&quot;find&quot; action=&quot;procs.asp&quot; method=post>
<P><INPUT id=text1 name=txtName> </P>
<P><INPUT id=submit1 name=submit1 type=submit value=Find></P>

</form>
</body>
</html>
this question was asked awhile back and this thread may help you in understanding this better. My explanation went into it a little farther.
thread329-317980

hope taht helps


A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top