Link to home
Start Free TrialLog in
Avatar of Eduski
Eduski

asked on

Help with cfform validation

I have a registration process that loops through different options to present them to the user for selection of either "Yes" or "No" in a cfform.  The code is attached below.

Some details:
option_id is an integer

The problem I'm having is that even if I select "Yes" or "No" from the item list, the coldfusion keeps throwing an alert at me "You must select an answer for each addon group."  I don't know why it won't validate this form.  

I have attached my code as well as the code coldfusion outputs to validate the form when the site is live.  The form field in question is numbered "1" for the option_id.  Any ideas?

Thanks.
<script type="text/javascript">
<!--
    _CF_checkCFForm_1 = function(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;


        //display error messages and return success
        if( _CF_error_exists )
        {
            if( _CF_error_messages.length > 0 )
            {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->

</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['mailingemail']=true;
/* ]]> */</script>
<script type="text/javascript">
<!--
    _CF_checkCFForm_2 = function(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;

        //form element mailingemail required check
        if( _CF_hasValue(_CF_this['mailingemail'], "TEXT", false ) )
        {
            //form element mailingemail 'EMAIL' validation checks
            if (!_CF_checkEmail(_CF_this['mailingemail'].value, true))
            {
                _CF_onError(_CF_this, "mailingemail", _CF_this['mailingemail'].value, "Invalid Email Address.  Please try again.");
                _CF_error_exists = true;
            }

        }else {
            _CF_onError(_CF_this, "mailingemail", _CF_this['mailingemail'].value, "Invalid Email Address.  Please try again.");
            _CF_error_exists = true;
        }


        //display error messages and return success
        if( _CF_error_exists )
        {
            if( _CF_error_messages.length > 0 )
            {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->
</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['division_selected']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['division_selected']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['division_selected']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['division_selected']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['division_selected']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['1']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['1']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['payment_method']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['payment_method']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['payment_amount']=true;
/* ]]> */</script>

<script type="text/javascript">/* <![CDATA[ */
	if (window.ColdFusion) ColdFusion.required['Agree']=true;
/* ]]> */</script>
<script type="text/javascript">
<!--
    _CF_checkCFForm_3 = function(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element 1 required check
        if( !_CF_hasValue(_CF_this['1'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "1", _CF_this['1'].value, "You must select an answer for each open addon group.");
            _CF_error_exists = true;
        }

        //form element 1 required check
        if( !_CF_hasValue(_CF_this['1'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "1", _CF_this['1'].value, "You must select an answer for each open addon group.");
            _CF_error_exists = true;
        }

        //form element payment_method required check
        if( !_CF_hasValue(_CF_this['payment_method'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_method", _CF_this['payment_method'].value, "Please indicate your payment method.");
            _CF_error_exists = true;
        }

        //form element payment_method required check
        if( !_CF_hasValue(_CF_this['payment_method'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_method", _CF_this['payment_method'].value, "Please indicate your payment method.");
            _CF_error_exists = true;
        }

        //form element payment_amount required check
        if( !_CF_hasValue(_CF_this['payment_amount'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_amount", _CF_this['payment_amount'].value, "Please indicate your payment amount.");
            _CF_error_exists = true;
        }

        //form element Agree required check
        if( !_CF_hasValue(_CF_this['Agree'], "CHECKBOX", false ) )
        {
            _CF_onError(_CF_this, "Agree", _CF_this['Agree'].value, "Please Read and Agree to the Commitments.");
            _CF_error_exists = true;
        }


        //display error messages and return success
        if( _CF_error_exists )
        {
            if( _CF_error_messages.length > 0 )
            {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->
</script>

Open in new window

<cfquery datasource="#db_source#" name="getaddons">select * from user_registration_options where option_fee_id = #fc# and option_eligibility = 'Open'</cfquery>
<cfif getaddons.recordcount gt 0>
   <cfloop query="getaddons"><tr>
           <td class="accounttd" width="40%">Item:<strong>#option_title#</strong><br /></td>
           <td valign="top" class="account_notify">		   
             <strong>Would you like to register for this add on?</strong>
            <cfinput type="radio" name="#option_id#" value="Yes" required="no" message="You must select an answer for each addon group." />Yes
               <cfinput type="radio" name="#option_id#" value="No" required="no" message="You must select an answer for each addon group." />No</div></td>
         </tr></cfloop>
		 <cfelse>
		           <tr> <td colspan="2" class="accounttd">There are no "open" addons for this registration item.  No selection is needed.</td></tr>
		 </cfif>

Open in new window

Avatar of onlyamir007
onlyamir007

try this .....

are you validating these radio??? or anything else???
<cfquery datasource="#db_source#" name="getaddons">select * from user_registration_options where option_fee_id = #fc# and option_eligibility = 'Open'</cfquery>
<cfif getaddons.recordcount gt 0>
   <cfloop query="getaddons"><tr>
           <td class="accounttd" width="40%">Item:<strong>#option_title#</strong><br /></td>
           <td valign="top" class="account_notify">		   
             <strong>Would you like to register for this add on?</strong>
            <cfinput type="radio" name="#option_id#" value="Yes"  />Yes
               <cfinput type="radio" name="#option_id#" value="No"  />No</div></td>
         </tr></cfloop>
		 <cfelse>
		           <tr> <td colspan="2" class="accounttd">There are no "open" addons for this registration item.  No selection is needed.</td></tr>
		 </cfif>

Open in new window

By default, required attribute in cfinput is no, so you don't really need to specify it..

<cfinput type="radio" name="#option_id#" value="Yes" required="no" message="You must select an answer for each addon group.">Yes
<cfinput type="radio" name="#option_id#" value="No" required="no" message="You must select an answer for each addon group.">No

Don't enter  "/>" end tag in your cfinput... they have the same name...
Avatar of Eduski

ASKER

Thanks for your replies.  Neither of those suggestions achieved my desired effect.  I need the form to validate each radio group in the loop, but for some reason it keeps asking even when a selection in that group is made.  The code outputs in the browser as follows (attached). and the form validation is as follows (attached).  Thanks for your help.
<script type="text/javascript">
<!--
    _CF_checkCFForm_3 = function(_CF_this)
    {
        //reset on submit
        _CF_error_exists = false;
        _CF_error_messages = new Array();
        _CF_error_fields = new Object();
        _CF_FirstErrorField = null;

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element division_selected required check
        if( !_CF_hasValue(_CF_this['division_selected'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "division_selected", _CF_this['division_selected'].value, "You must select a division/age group.");
            _CF_error_exists = true;
        }

        //form element 1 required check
        if( !_CF_hasValue(_CF_this['1'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "1", _CF_this['1'].value, "You must select an answer for each open addon group.");
            _CF_error_exists = true;
        }

        //form element 1 required check
        if( !_CF_hasValue(_CF_this['1'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "1", _CF_this['1'].value, "You must select an answer for each open addon group.");
            _CF_error_exists = true;
        }

        //form element payment_method required check
        if( !_CF_hasValue(_CF_this['payment_method'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_method", _CF_this['payment_method'].value, "Please indicate your payment method.");
            _CF_error_exists = true;
        }

        //form element payment_method required check
        if( !_CF_hasValue(_CF_this['payment_method'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_method", _CF_this['payment_method'].value, "Please indicate your payment method.");
            _CF_error_exists = true;
        }

        //form element payment_amount required check
        if( !_CF_hasValue(_CF_this['payment_amount'], "RADIO", false ) )
        {
            _CF_onError(_CF_this, "payment_amount", _CF_this['payment_amount'].value, "Please indicate your payment amount.");
            _CF_error_exists = true;
        }

        //form element Agree required check
        if( !_CF_hasValue(_CF_this['Agree'], "CHECKBOX", false ) )
        {
            _CF_onError(_CF_this, "Agree", _CF_this['Agree'].value, "Please Read and Agree to the Commitments.");
            _CF_error_exists = true;
        }


        //display error messages and return success
        if( _CF_error_exists )
        {
            if( _CF_error_messages.length > 0 )
            {
                // show alert() message
                _CF_onErrorAlert(_CF_error_messages);
                // set focus to first form error, if the field supports js focus().
                if( _CF_this[_CF_FirstErrorField].type == "text" )
                { _CF_this[_CF_FirstErrorField].focus(); }

            }
            return false;
        }else {
            return true;
        }
    }
//-->
</script>

Open in new window

<form name="CFForm_3" id="CFForm_3" action="account_register.cfm?fc=1" method="post" onsubmit="return _CF_checkCFForm_3(this)">
           <td valign="top" class="account_notify">		   
             <div align="right"><strong>Would you like to register for this add on?</strong>
  <input name="1" id="1"  type="radio" value="Yes" />Yes
  <input name="1" id="1"  type="radio" value="No" />No</div></td>
</form>

Open in new window

You can try using JS and input type..

here is a similar question on EE on how to do it..

https://www.experts-exchange.com/questions/20742824/Required-Radio-Buttons-don't-alert-in-order.html
ASKER CERTIFIED SOLUTION
Avatar of Brijesh Chauhan
Brijesh Chauhan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduski

ASKER

I wonder why this is...