I am working with some old code. It uses the built-in form validation ColdFusion (i.e. _requiredhidden fields). I want to add a cancel button to the form. The Cancel button should actually handle some business logic (therefore, I cannot just set its location.href to another page). The problem is that since the cancel button is a submit button, it starts the built-in check and the user receives the error message that is required for this field.
Is there a way to disable validation for this submit button? I would prefer not to try to change the base code that forms the form, as it is used in several places. Here is a very simplified version of my code:
<cfif IsDefined("Form.OK")>
You clicked OK!
<cfelseif IsDefined("Form.Cancel")>
You clicked Cancel!
</cfif>
<cfoutput>
<form action="#CGI.Path_Info#" method="POST">
Enter Name: <input type="text" name="Name" value="" /><br/>
<input type="hidden" name="Name_required" value="" />
<input type="submit" name="OK" value="OK" />
<input type="submit" name="Cancel" value="Cancel" />
</form>
</cfoutput>
, , - onclick "" "_required" DOM. , . Javascript, :
<script type="text/javascript">
function removeRequiredFields() {
var els = document.getElementsByTagName('input');
for(var i = 0; i <= els.length; i++) {
if(els[i].type == 'hidden' && els[i].name.endsWith('_required'))
els[i].parentNode.removeChild(els[i]);
}
}
</script>