How to check whether all the TextFileds and RadioButtons are selected?

How to check whether all the TextFileds and RadioButtons are selected?

how to get selected radio button value in javascript
check if radio button is selected jquery
how to get selected radio button text in javascript
check if radio button is selected c#
radio button checked condition in jquery
radio button checked javascript
check if radio button is selected android
javascript select radio button based on value

If and only if all the TextFileds filled and RadioButtons are selected the else part has to execute. otherwise Alert should pop-up. but, my code is not working as expected.

After entering all the values the alert message till popping up you can see in the image. Click on the image below to open the output image. Please help.

sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="485.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <Label layoutX="234.0" layoutY="14.0" prefHeight="27.0" prefWidth="133.0" text="Bismillah Fruits" textFill="#f20404">
            <font>
                <Font name="System Bold" size="18.0" />
            </font>
        </Label>
        <Label layoutX="56.0" layoutY="72.0" text="Name:">
            <font>
                <Font name="System Italic" size="14.0" />
            </font>
        </Label>
        <TextField fx:id="name" layoutX="109.0" layoutY="69.0" promptText="Please Enter Name" />
        <DatePicker fx:id="date" layoutX="411.0" layoutY="69.0" promptText="Select Date" />
        <ChoiceBox fx:id="choiceBox1" layoutX="40.0" layoutY="153.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs1" layoutX="160.0" layoutY="153.0" promptText="value" />
        <TextField fx:id="mul1" layoutX="331.0" layoutY="153.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <ChoiceBox fx:id="choiceBox2" layoutX="38.0" layoutY="198.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs2" layoutX="160.0" layoutY="198.0" promptText="value" />
        <TextField fx:id="mul2" layoutX="331.0" layoutY="198.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <ChoiceBox fx:id="choiceBox3" layoutX="40.0" layoutY="236.0" prefHeight="25.0" prefWidth="112.0" />
        <TextField fx:id="kgs3" layoutX="160.0" layoutY="236.0" promptText="value" />
        <TextField fx:id="mul3" layoutX="331.0" layoutY="236.0" prefHeight="25.0" prefWidth="36.0" promptText="box" />
        <Separator layoutX="411.0" layoutY="131.0" orientation="VERTICAL" prefHeight="300.0" prefWidth="4.0" />
        <Label layoutX="485.0" layoutY="122.0" text="Total" />
        <Label layoutX="155.0" layoutY="122.0" text="Details">
            <font>
                <Font name="System Italic" size="12.0" />
            </font>
        </Label>
        <Separator layoutX="26.0" layoutY="138.0" prefHeight="4.0" prefWidth="543.0" />
        <Label layoutX="315.0" layoutY="157.0" text="x" />
        <Label layoutX="315.0" layoutY="202.0" prefHeight="17.0" prefWidth="6.0" text="x" />
        <Label layoutX="315.0" layoutY="240.0" text="x" />
        <Label fx:id="value1" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="157.0" text="0" />
        <Label fx:id="value2" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="202.0" text="0" />
        <Label fx:id="value3" alignment="CENTER" contentDisplay="CENTER" layoutX="495.0" layoutY="240.0" text="0" />
        <Separator layoutX="421.0" layoutY="419.0" prefHeight="4.0" prefWidth="167.0" />
        <Label layoutX="43.0" layoutY="335.0" text="Advance:" />
        <Label layoutX="43.0" layoutY="368.0" text="Coolie:" />
        <Label layoutX="43.0" layoutY="403.0" text="Market:" />
        <Label layoutX="42.0" layoutY="303.0" text="Commission:" />
      <RadioButton fx:id="eight" layoutX="167.0" layoutY="303.0" mnemonicParsing="false" text="8%" />
      <Label layoutX="211.0" layoutY="303.0" text="or" />
      <RadioButton fx:id="ten" layoutX="228.0" layoutY="303.0" mnemonicParsing="false" text="10%" />
      <Label fx:id="value4" alignment="CENTER" contentDisplay="CENTER" layoutX="469.0" layoutY="303.0" text="Commission" />
        <TextField fx:id="advance" layoutX="160.0" layoutY="331.0" promptText="Advance" />
        <TextField fx:id="coolie" layoutX="160.0" layoutY="364.0" prefHeight="25.0" prefWidth="64.0" promptText="Coolie" />
      <Label layoutX="230.0" layoutY="368.0" text="x" />
      <RadioButton fx:id="three" layoutX="240.0" layoutY="368.0" mnemonicParsing="false" text="3" />
      <Label layoutX="270.0" layoutY="368.0" text="or" />
      <RadioButton fx:id="five" layoutX="285.0" layoutY="368.0" mnemonicParsing="false" text="5" />
      <Label fx:id="value5" alignment="CENTER" contentDisplay="CENTER" layoutX="480.0" layoutY="368.0" text="Coolie" />
        <TextField fx:id="market" layoutX="160.0" layoutY="399.0" promptText="Market charge" />
        <Label fx:id="total" alignment="CENTER" contentDisplay="CENTER" layoutX="485.0" layoutY="423.0" text="Total" />
      <Separator layoutX="425.0" layoutY="333.0" prefHeight="4.0" prefWidth="167.0" />
      <Separator layoutX="425.0" layoutY="280.0" prefHeight="4.0" prefWidth="167.0" />
        <Button fx:id="cal" layoutX="208.0" layoutY="440.0" mnemonicParsing="false" text="Calculate" />
    </children>
</Pane>

and controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    private int total43;
    private static final String ORANGE = "Orange";
    private static final String APPLES = "Apples";
    private static final String MANGO = "Mango";
    private static final String CORN = "Corn";

    @FXML
    public Pane pane;

    @FXML
    public TextField name;

    @FXML
    public DatePicker date;

    @FXML
    public TextField kgs1, kgs2, kgs3, mul1, mul2, mul3, advance, coolie, market;

    @FXML
    public Label value1, value2, value3, value4, value5, total;

    @FXML
    public Button cal;

    @FXML
    public ChoiceBox choiceBox1, choiceBox2, choiceBox3;

    @FXML
    public RadioButton three, five, eight, ten;

    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    private ToggleGroup toggleGroup, toggleGroup2;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        System.out.println("Inside initialize.");
        choiceBox1.getItems().add(ORANGE);
        choiceBox1.getItems().add(APPLES);
        choiceBox1.getItems().add(MANGO);
        choiceBox1.getItems().add(CORN);

        System.out.println("choiceBox2 items added.");
        choiceBox2.getItems().add(ORANGE);
        choiceBox2.getItems().add(APPLES);
        choiceBox2.getItems().add(MANGO);
        choiceBox2.getItems().add(CORN);

        System.out.println("choiceBox3 items added.");
        choiceBox3.getItems().add(ORANGE);
        choiceBox3.getItems().add(APPLES);
        choiceBox3.getItems().add(MANGO);
        choiceBox3.getItems().add(CORN);

        choiceBox1.setValue(ORANGE);
        choiceBox1.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice1 clicked");
        });

        choiceBox2.setValue(APPLES);
        choiceBox2.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice2 clicked");
        });

        choiceBox3.setValue(MANGO);
        choiceBox3.getSelectionModel().selectedItemProperty().addListener((observableValue, o, t1) -> {
            System.out.println("choice3 clicked");
        });

        //
        System.out.println("ToggleGroup 8 or 10 object creation begin.");
        toggleGroup = new ToggleGroup();
        System.out.println("ToggleGroup 8 or 10 object created.");
        eight.setToggleGroup(toggleGroup);
        ten.setToggleGroup(toggleGroup);
        //
        System.out.println("ToggleGroup2 3 or 5 object creation begin.");
        toggleGroup2 = new ToggleGroup();
        System.out.println("ToggleGroup2 3 or 5 object created.");
        three.setToggleGroup(toggleGroup2);
        five.setToggleGroup(toggleGroup2);

        cal.setOnAction(actionEvent -> {
            System.out.println("cal function called. ");
            cal();
            System.out.println("cal function ended. ");
        });
    }

    public boolean isTrue(){
        if (!(value1.getText().trim().isEmpty() && value2.getText().trim().isEmpty() &&
                value3.getText().trim().isEmpty() && mul1.getText().trim().isEmpty() &&
                mul2.getText().trim().isEmpty() && mul3.getText().trim().isEmpty() &&
                advance.getText().trim().isEmpty() && coolie.getText().trim().isEmpty() &&
                market.getText().trim().isEmpty() && name.getText().trim().isEmpty() &&
                date.getValue() != null && toggleGroup.getSelectedToggle().isSelected() &&
                toggleGroup2.getSelectedToggle().isSelected())) {

            System.out.println("inside if ");
            alert.setTitle("Information");
            System.out.println("alert.setTitle(Error)");
            alert.setHeaderText("Error Occurred");
            System.out.println("alert.setHeaderText(Error Occurred);");
            alert.setContentText("Please Enter All Values");
            System.out.println("alert.setContentText(Please Enter All Values)");
            alert.setAlertType(Alert.AlertType.INFORMATION);
            System.out.println("alert.setAlertType(Alert.AlertType.ERROR)");
            alert.show();
            System.out.println("alert.show()");

            return false;

        }

        return true;

    }

    private void cal() {

        float comm, comm2;

        if (isTrue()) {

            System.out.println("Else begins. ");
            value1.setText(String.valueOf(Integer.parseInt(kgs1.getText()) * Integer.parseInt(mul1.getText())));
            value2.setText(String.valueOf(Integer.parseInt(kgs2.getText()) * Integer.parseInt(mul2.getText())));
            value3.setText(String.valueOf(Integer.parseInt(kgs3.getText()) * Integer.parseInt(mul3.getText())));
            total43 = Integer.parseInt(value1.getText()) +
                    Integer.parseInt(value2.getText()) +
                    Integer.parseInt(value3.getText());
            //
            total.setText(String.valueOf(Integer.parseInt(value1.getText()) +
                    Integer.parseInt(value2.getText()) +
                    Integer.parseInt(value3.getText())));
            System.out.println("Total value " + total43);

            // -------------------------------------
            System.out.println("8 or 10 if begin.");
            if (eight.isSelected()) {
                comm = (total43 * 8) / 100;
                comm2 = total43 - comm;
                value4.setText(String.valueOf(comm2));
            } else {
                comm = (total43 * 10) / 100;
                comm2 = total43 - comm;
                value4.setText(String.valueOf(comm2));
            }
            System.out.println("8 or 10 if closed.");

            System.out.println("advance " + advance.getText());
            //
            System.out.println("3 or 5 if begin.");
            if (three.isSelected()) {
                value5.setText(String.valueOf(Integer.parseInt(coolie.getText()) * 3));
            } else
                value5.setText(String.valueOf(Integer.parseInt(coolie.getText()) * 5));
            System.out.println("3 or 5 if closed.");
            //
            System.out.println("market value.");
            System.out.println("market value: " + market.getText());
        }

    }
}

After entering all the values the alert message till popping up. Thank you in advance.


You're entering the if, if not all of the conditions are true. E.g. if at least one of the TextFields is non-empty the expression inside the ()s is false resulting in the condition evaluating to true. You need to remove the negation and replace the ands (&&) with ors ||.

I'd recommend making this code less repetitive though. If you add those elements to a collection or array you can simply iterate over them:

private List<TextField> textInputs;
private List<ToggleGroup> toggleGroups;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    ...
    textInputs = Arrays.asList(value1, value2, value3, mul1, mul2, mul3, advance, coolie, market, name);

    // after initializing the ToggleGroups
    toggleGroups = Arrays.asList(toggleGroup, toggleGroup2);
}

...

public boolean isTrue() { // choose a better name for this
    boolean valid = (date.getValue() != null);

    if (valid) {
        for (TextField tf : textInputs) {
            if (tf.getText().trim().isEmpty()) {
                valid = false;
                break;
            }
        }
        if (valid) {
            for (ToggleGroup tg : toggleGroups) {
                if (tg.getSelectedToggle() == null) {
                    valid = false;
                    break;
                }
            }
        }
    }
    if (!valid) {
        // show alert here...
    }
    return valid;
}

This way you avoid repeating the check over and over again which makes it easier to change the check for all TextFields/ToggleGroups at once. Note that the selected toggle of a ToggleGroup is null, if none of the Toggles is in the selected state; your code would yield a NPE in this case.

I do recommend replacing your Pane with a GridPane btw: A GridPane is able to deal with differend control dimensions based on the font (size) used which can be different between OSs. Pane is not able to deal with this at all; it's simply using absolute positions independent of the size of it's children.

How to check whether a radio button is selected with JavaScript , querySelector('input[name=”GFG”]:checked') method to check the checked element of radio button and dislapy its corresponding result. If no� Given a form containing radio button element and the task is to check whether a radio button is selected or not with the help of JavaScript. There are two methods to solve this problem which are discussed below: Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button.


I believe the conditional you have within the isTrue() function is not what you want it to be. You probably want to say something like: if(value1.getText().trim().isEmpty() || value2.getText().trim().isEmpty()...)

Also the condition date.getValue() != null (false when empty) has the opposite parity to all the other conditions (they are true when empty).

JavaScript Radio Button, Summary. Use the name attribute of the <input type="radio"> element to form a group of radio buttons. Use checked property of the radio button to check if the radio button is checked. Radio buttons. Radio buttons are similar to checkboxes, except that only one radio button in a group can be selected at any one time. (As with checkboxes, a radio button group is a collection of radio buttons with the same name attribute.) Radio buttons are useful for getting a user to pick from a choice of options.


In your if statement I believe a better solution might be to use TextField.length()==0 and get rid of the blanket ! to avoid confusion in reading the code.

HTML DOM Input Radio checked Property, Check and un-check a specific radio button: Find out if a radio button is checked or not: Use a radio button to convert text in an input field to uppercase: . A user cannot manually uncheck a radio button or a selected button within an exclusionary group of radio buttons once one of the buttons in the group has been selected except for the use of JavaScript of resetting the group of radio buttons. Check boxes on the other had can be manually deselected.


How to Check if One Radio Button in Each Group is Checked using , In the example script, there are three questions and each question has two radio buttons to select an option. Now before submitting the user input,� Download Free Files API. In this article I will explain with an example, how to check if a RadioButton or RadioGroup (i.e. set of RadioButtons) is checked (selected) or not checked (not selected) using jQuery. When the RadioButton is clicked based on whether it is checked (selected) or unchecked (unselected), the HTML DIV will be shown or hidden using jQuery.


Exposing Additional Form Fields via Checked Radio Buttons, Exposing Additional Form Fields via Checked Radio Buttons They aren't all required just to hide it, but they will play a factor in how it reveals itself. value, you run some JavaScript to determine whether that input should be required or not. Inside the Validate JavaScript function, first the reference of the RadioButtonList is determined and then all the RadioButtons inside the RadioButtonList are referenced. Then a loop is executed over the referenced RadioButtons and each RadioButton is validated whether it is checked or not.


<input type="radio">, input elements of type radio are generally used in radio of them all, whereas checkboxes allow multiple values to be selected. Once a radio group is established, selecting any radio button in that Let's now look at the other common radio-button-related features and techniques you may need to know� Check with other sample sites its working or not because i don’t think So it is an issue with chrome and all . Can you share that link if possible or something that will lead to make an assumption. Regards…!!