简体   繁体   中英

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?

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>

您可以使用:-

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

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.

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>

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM