Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

How to cancel a form's submit event

<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
calven
Asked:
calven
  • 4
  • 3
  • 2
  • +1
1 Solution
 
drittichCommented:
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
 
ckanetaCommented:
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
 
megaastarCommented:
Put this code in Else section thatz it!!


frm_SL_onsubmit = False


Bye!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
megaastarCommented:
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
 
ckanetaCommented:
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
 
drittichCommented:
ckaneta - you are right, I was modifying Calven's code and I missed that
0
 
ckanetaCommented:
them damned brackets are easy to miss!
happens to me all the time
0
 
calvenAuthor Commented:
<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
 
calvenAuthor Commented:
<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
 
megaastarCommented:
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
 
megaastarCommented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now