sareeshmnair
sareeshmnair

Reputation: 431

jsp two buttons in a form and javascript form validation

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.

Upvotes: 0

Views: 1400

Answers (4)

Dinesh Developer
Dinesh Developer

Reputation: 1

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

Upvotes: -1

Sadun89
Sadun89

Reputation: 11

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

Upvotes: 0

jmail
jmail

Reputation: 6132

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'" />

Upvotes: 0

Sridhar R
Sridhar R

Reputation: 20428

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" />

Upvotes: 4

Related Questions