Solved

How to cancel a form's submit event

Posted on 2001-06-06
11
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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