Solved

How to cancel a form's submit event

Posted on 2001-06-06
11
401 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

8 Experts available now in Live!

Get 1:1 Help Now