Madhusudan
Madhusudan

Reputation: 4815

How to disable form validation for some submit button?

In my jsp form page, I have two submit button "Save" and "Cancel". When I click on both buttons it validates the form. I tried to put keyword "formnovalidate" for cancel button. But its not working.

Here I mentioned my button code:

<form id = "myForm" method="POST" onsubmit="return validateForm()" >
..........
<tr >
<td class="td_left"><input type="submit" value="save" onclick="form.action='${pageContext.servletContext.contextPath}/insert'"/></td>
<td class="td_right"><input type="submit" value="Cancel" onclick="form.action='${pageContext.servletContext.contextPath}/home'" formnovalidate  /></td>
</tr>
................
</form>

Validations:

function validateForm() {
var x = document.forms["myForm"]["spcrId"].value;
if (x == null || x == "") {
    alert("SPCR ID must be filled out");
    return false;
}
}

What is the way to disable form validations for "Cancel" button?

Upvotes: 3

Views: 12084

Answers (5)

istvanp
istvanp

Reputation: 4443

Try this:

<input type="submit" value="Cancel" onclick="this.form.setAttribute('novalidate', 'novalidate'); form.action='${pageContext.servletContext.contextPath}/home'"  />

By setting the form's novalidate attribute on the click event you can bypass the validation for only that button.

Upvotes: 2

Aman Soni
Aman Soni

Reputation: 85

You can use:-

  `<button type="button"></button>`

Upvotes: -1

Fernando Wolff
Fernando Wolff

Reputation: 216

I think the problem was because you, on the form's onsubmit attribute, placed a validation call. So, something like this should work without that onsubmit attribute:

<button name="action" class="btn btn-secondary" type="submit" value="Previous" formnovalidate="formnovalidate">Previous</button>

Upvotes: 8

Rahul Ahirrao
Rahul Ahirrao

Reputation: 319

try this

var myFormVar = document.myForm.FieldName.value;
if ((myFormVar == "") ) {
   document.myForm.FieldName.value = ''; 
   document.myForm.FieldName.focus();
   alert ("alert here");
   return;
}

Upvotes: 0

Sumanta736
Sumanta736

Reputation: 705

Instead of type = "submit" you have to use

 <input type="button" value="Cancel" />

or you can use

<button type="cancel" onclick="window.location='http://your.com';return
false;">Cancel</button>

Upvotes: 2

Related Questions