Validation in radio button selected or not in IOS

Hamza Imran

i have made three radio buttons rd1 , rd2 and rd3. When user click rd1 it shows some buttons under it from which we have to select anyone . Same senario in three radio buttons. Now i want to put validation on radio buttons. When the page load no radio is yet selected and when user want to go to next page it should first select any radio button and a option under it. I have tried some code but its not working properly. When i click next button it show alert that Enter Property Type. But when i select any radio button and option under it , its again show me same alert after selection also and i can't move to the next page. My CODE IS,

 else if ([rd1 isSelected]==NO || [rd2 isSelected]==NO || [rd3 isSelected]==NO){

    if ([rd1 isSelected]==NO || [rd2 isSelected]==NO || [rd3 isSelected]==NO) {
        NSString *message = @"Property Type";

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Select Property Type"
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert];

        [self presentViewController:alert animated:YES completion:nil];

        int duration = 1; // duration in seconds

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });

    }
    else{
        if ([rd1 isSelected]==YES || [rd2 isSelected]==YES || [rd3 isSelected]==YES){


            [rd1.titleLabel.text isEqualToString:@"Homes"];
            [rd2.titleLabel.text isEqualToString:@"Button"];
            [rd3.titleLabel.text isEqualToString:@"Button"];
        }

    }

    }
Nirmalsinh

There is an issue in your condition. If you want to check if at least one button should be clicked then you need to put && condition:

   if(rd1.selected == false && rd2.selected == false && rd3.selected == false)
    {
        // You need to show alert because none of the radio buttons is selected.
        // Show your alert
    }
    else
    {
        // One of them is selected...

    }

Condition Explaination:

&& : Each and every condition which you have written must need to satisfy. If so then it will give you TRUE, otherwise FALSE.

|| : If any of condition is satisfied then it will give you TRUE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related