开发者

Extending Coldfusion CFFORM Validation with Custom JavaScript Validation

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?


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
  } 


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜