Reputation: 57
Does anyone know how to combine custom Javascript validation with the built in Coldfusion validation, so the custom Javascript alert is rolled into the same alert box as the CF one? I know CF adds an onsubmit event to the form and creates a _CF_check['name of form'] function to do some js validation, but how would I combine the alerts?
Upvotes: 0
Views: 3107
Reputation: 10702
I've actually moved away from using CFFORM's built-in validation mechanism, and CFFORM overall, by creating validation logic in CFC methods, and calling these remotely using jQuery.ajax()
. This allows you to keep your validation in one place and enables you to use it for both client-side and server-side validation.
Upvotes: 0
Reputation: 28873
As mentioned in the comments, you could use the onValidate
attribute to call a custom javascript function. Not quite as elegant as using a cfselect (which you said you cannot use). But it does combine the alerts, and without hacking into the internal functions. If for some reason you cannot use onValidate, then you probably will have to dig into the internals.
<script type="text/javascript">
function yourFunction(frm, fld, value){
// some pointless validation
var elem = document.getElementById('foo');
return elem.options[elem.selectedIndex].value == 2;
}
</script>
<cfform name="theForm" method="post">
<select name="foo" id="foo">
<option value="0">apples</option>
<option value="1">oranges</option>
<option value="2">pear</option>
<option value="3">grape</option>
</select>
<cfinput type="hidden" name="fooValidate" onValidate="yourFunction"
message="You must select pears because we say so..">
<input type="Submit" name="txtSubmit">
</cfform>
Note: The signature of your javascript function must be:
function yourFunction(formObject, formField, fieldValue) {
...
// return true if validation was successful
}
Upvotes: 3
Reputation: 13847
I think the only way you can do it is to just call CF build-in javascript function from your customized function like following code.
<script>
function myFunction (_CF_this) {
.....
.....
_CF_checkmyForm(_CF_this) // here is CF built-in function
.....
.....
}
</script>
<cfform name="myForm">
<cfinput name="txtInput" required="Yes">
<input type="Submit" name="txtSubmit">
</cfform>
Upvotes: 0