0

I am working on a web project using structs2. I have a form with submit button and another button. I am using javascript functions for form validation. My problem is that when I click the other button the form validation function works. my jsp:

<h2>New Form</h2>
<s:form  action="aa.action" name="myform" method="post" onsubmit="return(validateForm())">
<s:textfield name="formnumber" size="20" />
.
.
<button value="add" name="add">Add</button>
.
.
<s:submit method="create" key="xxx" value="xxx"  />

When i click anyone of the the button the validation function will excecute.I dont want excecute the validation function on add button click.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
sareeshmnair
  • 441
  • 1
  • 6
  • 18

4 Answers4

4

Default button always submit the form so add type="button" it will stop

<button type="button" value="add" name="add">Add</button>

OR Use

<input type="button" name="add" value="Add" />
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • 1
    Another solution is to use ``, [that gives less problems](http://stackoverflow.com/a/469084/1654265). – Andrea Ligios Apr 07 '14 at 11:40
  • @AndreaLigios i know but OP code use button thats why i am not mentioning that,thanks for your update `:-)` – Sridhar R Apr 07 '14 at 11:41
  • 1
    Feel free to add it to your answer as a second option. The fact that OP is not mentioning it may be because he doesn't know that, instead of being a requirement to use a button ;) – Andrea Ligios Apr 07 '14 at 11:43
0

Get rid of the onclick. You don't need it here. The type="add" already submits the form. Your concrete problem is likely caused because the onclick of the <form> has returned false.

<input type="button" name="method" value="Add" class="button"
    onclick="location.href='Employeelist.jsp'" />
jmail
  • 5,944
  • 3
  • 21
  • 35
0

You should mention type="button".
Otherwise it will submit the current URL again.

<button type="button" value="Add" name="add"/>

details about "type" in html
http://www.w3schools.com/tags/att_button_type.asp

Java Script Form validation can be
w3schools
Tutorials point
javaScript Coder

if you familier with java script libraries you can use validate.js

Sadun89
  • 11
  • 3
-1

type : What type of widget you want. vlaue : The text what you want in that. name : The id of that purticulat widget.