2 Replies Latest reply on Nov 18, 2009 12:02 PM by Gregory Lafrance

    How to validate a radiobutton group?

    webvalue Level 1
      How to validate a radiobutton group? Has anyone done it before?
        • 1. Re: How to validate a radiobutton group?
          choisington

          I have no idea. I am asking the same question. Let me know if you get it figured out.


          • 2. Re: How to validate a radiobutton group?
            Gregory Lafrance Level 6

            If this post answers your question or helps, please mark it as such.

             

            This code should answer your question:

             

            <?xml version="1.0"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                <![CDATA[
                  import mx.events.ValidationResultEvent;   
                  import mx.controls.Alert;
                  import mx.events.ItemClickEvent;
                  private function handleCard(event:ItemClickEvent):void {
                    if (event.currentTarget.selectedValue == "AmEx") {
                      Alert.show("You selected American Express."); 
                    } else if (event.currentTarget.selectedValue == "MC") {
                      Alert.show("You selected MasterCard.");
                    } else {
                      Alert.show("You selected Visa.");
                    } 
                  }
                  
                  private function handleValid(eventObj:ValidationResultEvent):void {
                    if(eventObj.type==ValidationResultEvent.INVALID){    
                      mx.controls.Alert.show("You must select a radio button.");
                      americanExpress.setStyle("color", "0xFF0000");
                      masterCard.setStyle("color", "0xFF0000");
                      visa.setStyle("color", "0xFF0000");          
                    }else{
                      americanExpress.setStyle("color", "0x000000");
                      masterCard.setStyle("color", "0x000000");
                      visa.setStyle("color", "0x000000");          
                    }
                  }      
                ]]>
              </mx:Script>
              <mx:Validator id="radioValidator" source="{cardtype}" 
                required="true" property="selectedValue" valid="handleValid(event)"
                invalid="handleValid(event)"/>
              <mx:RadioButtonGroup id="cardtype" itemClick="handleCard(event);"/>
              <mx:RadioButton groupName="cardtype" id="americanExpress" 
                value="AmEx" label="American Express" width="150"/>
              <mx:RadioButton groupName="cardtype" id="masterCard" 
                value="MC" label="MasterCard" width="150"/>
              <mx:RadioButton groupName="cardtype" id="visa" 
                value="Visa" label="Visa" width="150"/>
              <mx:Button label="Send" click="radioValidator.validate()"/>
            </mx:Application>