I have a CF page, main.cfm, for searching records based a various input fields. The search form's action page points back to itself (main.cfm).
<form id="frm_srch" action="main.cfm" method="post" onSubmit="return validate_search_form()">
<input id="order_date" name="OrderDate_srch" type="text"/>
I wired up the JS function validate_search_form() to onSubmit, and everything works ok except for the actual aborting of the page load.
function validate_search_form() {
var order_date = $("input[name='OrderDate_srch']").val();
var d = new Date(order_date);
if( d == 'Invalid Date' && order_date != "" ) {
alert("Invalid order date");
returnToPreviousPage();
//window.history.back();
//event.returnValue = false;
return(false);
}
alert("validations passed");
//event.returnValue = true;
return(true);
}
I also tried creating an event handler to simply always prevent the page load, like so
$('#frm_srch').submit(function (evt) {
evt.preventDefault();
window.history.back();
});
and everything else suggested in this post javascript to stop form submission
Is there anyway to make this happen from a CF page using a regular form? Using cfform/cfinput doesn't work in my case b/c I cannot get the date field to toggle as disabled/enabled correctly (it's associated to a checkbox, and this behavior is for another post)? Thanks.
This is part of your existing function:
if( d == 'Invalid Date' && order_date != "" ) {
alert("Invalid order date");
returnToPreviousPage();
return(false);
}
alert("validations passed");
return(true);
Here is a simpler version of js form validation:
if (something is wrong) {
display something;
return false;
}
else
return true;
The first difference is the call to function returnToPreviousPage(); If the problem is on the current page, this function call may not be serving any useful purpose. In fact, it might be messing you up.
The next difference is that your function does not have the keyword else after the if block. While it won't matter if the return false command in your if block executes, it doesn't do any harm and prevents the value from being returned inadvertently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With