Support » Plugin: Calculated Fields Form » How to check if any radio button options were checked?

  • Resolved linkin85

    (@linkin85)


    Hi, How can I checked if any of the radio button values were checked?

    for example: if I have radio button(gender) with 2 options e.g. male with value of 0 or female with value of 1.

    I don’t want to retrieve their values but only check if any has been selected.

    many thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello,

    You simply should use a conditional statement as part of the equation associated to the calculated field:

    
    (function(){
        var result;
        if(fieldname1 == 1)
        {
            /* Operations to calculate the result if female */
        }
        else
        {
            /* Operations to calculate the result if male */
        }
        return result;
    })()
    

    Best regards.

    Many thanks for the prompt reply, the issue is I have several radio buttons and the reason I want to validate if any has been checked is to ensure the calculate button will not display any value unless all required radio buttons has been selected.
    for example:
    Age: <10, 11-50, >51
    Gender: M, F
    Color: Red, Green, Blue.

    is there an easier way?
    p.s. when I checked ‘required’ under radio button, it doesn’t change anything, it will still automatically calculate.

    Plugin Author codepeople

    (@codepeople)

    Hello @linkin85

    Yes of course, including conditional statements or conditional operations as part of the equation. For example:

    
    (function(){
       var result = '';
       if(
            10 <= fieldname1 && fieldname1 <= 51 && 
            IN(fieldname2, ['M','F']) && 
            IN(fieldname3,['Red', 'Green', 'Blue'])
        )
        {
            /* Modify the value of result */
        }
        return result;
    })()
    

    If you need we implement your equation, we can offer you a custom coding service from our private website: Customization

    Best regards.

    Just to clarify:

    the radio option is returning value of 0 rather than NULL even if it hasn’t been selected, is this normal?

    Plugin Author codepeople

    (@codepeople)

    Hello @linkin85

    Yes, that’s correct, the plugin always tries to determine a valid number to be used in the mathematical operations, however, there is an alternative:

    Assuming the radio button field is the fieldname123, and you want to know if there is at least a ticked choice, you can use the following piece of code as part of the equation:

    
    getField(123).jQueryRef().find(':checked').length
    

    Best regards.

    just what I was looking for, many thanks indeed!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to check if any radio button options were checked?’ is closed to new replies.