Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

submitting by js and onsubmit handlers.

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
If you use js to submit a form, can you still capture onsubmit events? If I submit using an input button, the form validates; if I user js, it ignores the validation function and submits the form anyways.

Code:
<form name="datepicker" ... onsubmit="MM_validateForm('txtStartDate','','R','txtEndDate','','R');return document.MM_returnValue;">

<a href="javascript:document.datepicker.submit();" id="add" class="buttons"><span>add</span></a>

</form>
 
If you use js to submit a form, can you still capture onsubmit events?

Nope, here's a small test to try out for yourself:
Code:
<html>
<head>
<script type="text/javascript">

function validate() {
   alert("blah");
   return false;
}

</script>
</head>
<body>
<form id="blahForm" onsubmit="return validate()">
   <a href="javascript:document.getElementById('blahForm').submit()">submit me</a><br />
   <input type="submit" value="submit me" />
</form>
</body>
</html>

You will have to call the validation routine manually before you submit the form via javascript.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
This the best way to do it?

Code:
<html>
<head>
<script type="text/javascript">

function validate() {
   alert("blah");
   return true;
}

</script>
</head>
<body>
<form method="get" name="blahForm" id="blahForm">
   <a href="javascript:validate();document.getElementById('blahForm').submit()">submit me</a><br />
</form>
</body>
</html>
 
No, that will submit no matter what the validation routine returns, you'd want something like this if you want to submit with an anchor:
Code:
<html>
<head>
<script type="text/javascript">

function validate() {
   if (the form is not valid) {
      alert("something is screwed up");
      return false;
   }
   return true;
}

</script>
</head>
<body>
<form method="get" name="blahForm" id="blahForm">
   <a href="javascript:[!]if ([/!]validate()[!]) {[/!]document.getElementById('blahForm').submit()[!]}[/!]">submit me</a><br />
</form>
</body>
</html>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top