conditional required cfform field + customjavascript validation

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Re: conditional required cfform field + customjavascript validation

    Thanks for responding. Here's the code I'm using to do this. Perhaps it will
    allow a better understanding of what I'm trying to accomplish.

    I'm actually not using the "required" attribute for cfinput on the "Headline"
    field. I'm using the "onValidate" attribute to only run the javascript
    validation when the html table containing the field is visible, which is only
    when the user clicks the radio button for "Project Type" = "Press Release".

    Custom javascript for validating the Headline form field is not blank:

    <script language="JavaScript" type="text/javascript">
    function FormCheck()
    {
    if (document.PR.ProjectType.value=="Press Release" &&
    document.PR.Headline.value=="")
    {
    alert("Headline text required for Press Release email message.");
    return false;
    }
    }
    </script>


    Custom javascript function for showing/hiding the "Headline1" table containing
    the Headline field:

    <script language="JavaScript">
    function toggleme(targetID)
    {
    if (targetID.style.display=="none")
    targetID.style.display="";
    else
    targetID.style.display="none";
    }
    </script>


    Here's the code from the cfform where I call the toggleme javascript function:

    <cfinput type="radio" name="ProjectType" required="yes" message="Please Select
    a Project Type" value="Press Release" onClick="toggleme(Headline1)">Press
    Release


    Here's the html table which displays/hides when the radio button of value
    "Press Release" is selected:

    <table id="Headline1" style="display: none;">
    <tr valign="baseline">
    <td nowrap class="formLabelRequired" align="right" width="25%">Headline:
    </td>
    <td><cfinput class="formInput" type="text" name="Headline"
    value="#selectPRRec.Headline#" size="100" maxlength="255"
    onValidate="FormCheck" message="Headline text tequired for Press Relase Email
    message.">
    </td>
    </tr>
    </table>

    rockasocki Guest

  2. Similar Questions and Discussions

    1. WebAssist conditional validation
      Does anyone know if WebAssist's validation extension will allow a user to make a text field required only if another text field contains a certain...
    2. conditional required cfform field + custom javascriptvalidation
      The form I'm working on requires cfform to enable some eWebEditPro controls. Since I must use cfform, I'm also using it for form validation. The...
    3. CFFORM validation trumping Custom Validation
      Is there any way for custom form validation to work in concert with the cfform validation? I have a custom script that compares the values of two...
    4. CFFORM Validation trumping Custom Form Validation
      Is there any way for custom form validation to work in concert with the cfform validation? I have a custom script that compares the values of two...
    5. Netscape required field validation problems
      I have an interesting problem when I run the following code in Netscape (7.02) vs. IE. This page works great in IE and all my controls bring up the...
  3. #2

    Default Re: conditional required cfform field + customjavascript validation

    There's actually multiple issues here.

    1) AFAI, you need to reference individual elements of a radio button group as
    an array. The array elements are what hold the 'value' property.

    2) Your 'FormCheck' function is not returning 'true' if the validation is
    successful. In fact you're not returning anything, so the value it's passed is
    'undefined', which is not 'true'. Hence the error message all the time.

    3) You have an error alert in your javascript function as well as specified in
    the 'message' attribute to cfinput. Therefore, if it fails, you're going to be
    displaying 2 alerts.

    That being said, here is what I think your 'FormCheck' function should look
    like.


    function FormCheck()
    {
    var i;
    for (var i=0; i < document.PR.ProjectType.length; i++)
    {
    if (document.PR.ProjectType[i].checked)
    {
    if (document.PR.ProjectType[i].value=="Press Release" &&
    document.PR.Headline.value=="")
    {
    return false;
    }
    }
    }
    return true;
    }

    BSterner Guest

  4. #3

    Default Re: conditional required cfform field + customjavascript validation

    Thanks BSterner for your help! As it turns out I forgot to include "return
    true;" in my custom javascript... I included it and it now works as expected.
    I really appreciate your time and effort to help me figure out what was going
    wrong! I've replied to each of your points below.

    Originally posted by: BSterner
    There's actually multiple issues here.

    1) AFAIK, you need to reference individual elements of a radio button group as
    an array. The array elements are what hold the 'value' property (The exception
    to this is if you have a radio "group" w/only a single element).

    KR> I used a radio button group, so I don't need to use an array. Thanks for
    displaying the technique anyhow!

    2) Your 'FormCheck' function is not returning 'true' if the validation is
    successful. In fact you're not returning anything, so the value that's passed
    to 'onValidate' is 'undefined'. Hence the error message all the time.

    KR> Bingo! I forgot to include "return true;" in my custom javascript... I
    included it and it now works as expected.

    3) You have an error alert in your javascript function and you also specify it
    in the 'message' attribute to cfinput. Therefore, if it fails, you're going to
    be displaying 2 alerts.

    KR> This was a remnant of trial and error. Interestingly enough, the custom
    javascript alert is ignored. So unless I include a message="..." along with
    the OnValidate attribute in the <cfinput>, I get the default message "Error in
    headline text."

    That being said, here's what I think your 'FormCheck' function should look
    like.


    Again, thanks for demonstrating the technique you'd use. I'll keep it in my
    bag-o-trix!


    rockasocki Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139