Form validation w/ multiple buttons

A

Aaron C

I'm validating a form that has two buttons ("Next" and "Back"). The call
to the script is currently in the form's onSubmit() handler. Upon
pressing either button, the script runs. This is understandable, since
either button press is considered a submit. Is there a way to test which
button was pressed to determine whether or not to run the script?

Thanks,
Aaron

P.S. I've already tried attaching the same script to the "Next" button's
onSubmit() handler, and when doing so, the script is apparently never run.
 
M

McKirahan

Aaron C said:
I'm validating a form that has two buttons ("Next" and "Back"). The call
to the script is currently in the form's onSubmit() handler. Upon
pressing either button, the script runs. This is understandable, since
either button press is considered a submit. Is there a way to test which
button was pressed to determine whether or not to run the script?

Thanks,
Aaron

P.S. I've already tried attaching the same script to the "Next" button's
onSubmit() handler, and when doing so, the script is apparently never run.

One way is to set a variable via each Submit button's onClick then test it.

<html>
<head>
<title>onsubmit.htm</title>
<script type="text/javascript">
var bool;
function submits() {
bool ? alert("Prev") : alert("Next");
}
</script>
</head>
<body>
<form action="" method="post" onsubmit="return submits()">
<input type="submit" name="Prev" value="<=" onclick="bool=true">
<input type="submit" name="Next" value="=>" onclick="bool=false">
<form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top