Solved

How to cancel a form's submit event

Posted on 2001-06-06
11
392 Views
Last Modified: 2012-08-13
<script language=VBS>
function checking(myForm)
  if myForm.txtSINo.value = 1 then
    msgbox "Thanks."
  else
    <some problem here to cancel the submit event>
  end if
end function
</script>

<form name=frmSI action=chk_rec.asp method=post onSubmit="checking(frmSI)">
<font class="normal">SI: <input class="enable_textbox" type=text" name="txtSINo" maxlength="6">
<input class="button_side" type="submit" name="btnSubmit" value="Next">
</form>

This is the code that I used in my script. When the user click on the next button, I would like to perform some checking in the function called "checking". If the value of txtSINO is not 1 then I want to cancel the submit event (so that the form will not continue to submit to the ASP page in the server).

My problem here is, how to write the code to cancel it? I found a lot of javascript example to do it but I just want to use VBScript, is it possible? Thanks for your help.
0
Comment
Question by:calven
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:drittich
ID: 6160040
Try this:

<script language="javascript">
function checking(myForm) {
  if (myForm.txtSINo.value == 1) {
    alert('Thanks.');
    return true;
  else
    return false;
  }
}
</script>

<form name=frmSI action=chk_rec.asp method=post onSubmit="return checking(frmSI)">
<font class="normal">SI: <input class="enable_textbox" type=text" name="txtSINo" maxlength="6">
<input class="button_side" type="submit" name="btnSubmit" value="Next">
</form>
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 6160046
change the submit button to a normal button, then write an event handler for it

have the event handler check whatever it is you want to check and if it's okay, submit the form via
document.formname.submit();
otherwise don't submit the form

you could also use the onSubmit event of the form and have it return false if the data doesn't check out like you want it.
0
 

Accepted Solution

by:
megaastar earned 20 total points
ID: 6160635
Put this code in Else section thatz it!!


frm_SL_onsubmit = False


Bye!
0
 

Expert Comment

by:megaastar
ID: 6160656
Example:


If Len(FormMain.Contact_Email.Value) > 50                MsgBox "Email ID long (>50 characters)"
     FormMain_onsubmit = False    
End If

Suppose I validate a form named FormMain where a textbox is by name Contact_Email then I use above code to generate error and stop submission!!
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 6160779
OOh, and now that I take a second look at your code
I notice you already do that, so here's something else I saw
<script language="javascript">
function checking(myForm) {
 if (myForm.txtSINo.value == 1) {
   alert('Thanks.');
   return true;
 else
   return false;
 }
}
</script>
the brackets look wrong to me, should be something like:
<script language="javascript">
function checking(myForm) {
 if (myForm.txtSINo.value == 1) {
   alert('Thanks.');
   return true;
 } else {
   return false;
 }
}
</script>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:drittich
ID: 6161400
ckaneta - you are right, I was modifying Calven's code and I missed that
0
 
LVL 3

Expert Comment

by:ckaneta
ID: 6161484
them damned brackets are easy to miss!
happens to me all the time
0
 

Author Comment

by:calven
ID: 6161984
<script language=VBS>
       sub checking()
         msgbox frmSI.txtSINo.value
          if frmSI.txtSINo.value = "1" then
            frmSI_onsubmit = false
          else
            msgbox "submit"
          end if
       end sub
</script>

<form name=frmSI action=chk_rec.asp method=post onSubmit="checking">
<font class="normal">SI: <input class="enable_textbox" type=text" name="txtSINo" maxlength="6">
<input class="button_side" type="submit" name="btnSubmit" value="Next">
</form>

Megaastar,

I tried the code you suggested and I had modified my code as above. I found that it actually run into "frmSI_onsubmit = false" but the form just continue to submit to another page. Can you help me to just modify the code above and let me try again. Thanks very much.
0
 

Author Comment

by:calven
ID: 6161997
<script language=VBS>
       sub checking()
         msgbox frmSI.txtSINo.value
          if frmSI.txtSINo.value = "1" then
            frmSI_onsubmit = false
          else
            msgbox "submit"
          end if
       end sub
</script>

<form name=frmSI action=chk_rec.asp method=post onSubmit="checking">
<font class="normal">SI: <input class="enable_textbox" type=text" name="txtSINo" maxlength="6">
<input class="button_side" type="submit" name="btnSubmit" value="Next">
</form>

Megaastar,

I tried the code you suggested and I had modified my code as above. I found that it actually run into "frmSI_onsubmit = false" but the form just continue to submit to another page. Can you help me to just modify the code above and let me try again. Thanks very much.
0
 

Expert Comment

by:megaastar
ID: 6162118
Just do this:

Add this to the form definition and thatz it!!

LANGUAGE="VBSCRIPT"

Your code now looks like...
<form name=frmSI action=chk_rec.asp method=post onSubmit="checking" LANGUAGE="VBSCRIPT">

0
 

Expert Comment

by:megaastar
ID: 6162129
If you still do not get that,

Put your function as:

Function frmSl_onsubmit()
..
..
..
End Function

<form name=frmSI action=chk_rec.asp method=post LANGUAGE="VBSCRIPT">

This will 100% work!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now