Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to cancel a form's submit event

Posted on 2001-06-06
11
Medium Priority
?
427 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 40 total points
ID: 6160635
Put this code in Else section thatz it!!


frm_SL_onsubmit = False


Bye!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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/…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 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