JavaFX Email Validation using JFoenix TextField

Javafx email validation box

I encourage you to use Jfoenix material UI for developing JavaFX GUI, I've been using it for a while and been really happy, it's cool library, more people should use it so this cool project will continue to alive, this project been so slow in progress lately. You can use this library, put in your pom.xml dependencies tag.

<dependency>
    <groupId>com.jfoenix</groupId>
    <artifactId>jfoenix</artifactId>
    <version>8.0.1</version>
</dependency>


Use the right version, that version above is for Java 8 and below, if you are using Java 9 and above use this version instead:

<dependency>
    <groupId>com.jfoenix</groupId>
    <artifactId>jfoenix</artifactId>
    <version>9.0.9</version>
</dependency>


What we're gonna do is using JFoenix JFXTextField to make a box that if the user is input the wrong email format, if it's not a valid email format, there will be a red mark at the bottom of the box saying that it is not a valid email. So here's full example of how to validate email in JavaFX text field.


import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.RegexValidator;
import com.pusiknas.web.util.EmailValidator;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class EmailValidation extends Application {

    private EmailValidator emailValidator;
    private JFXTextField textField;

    public void emailBox(JFXTextField textField1){
        this.textField = textField1;
        this.doEmailValidate();

        RegexValidator valid = new RegexValidator();

        valid.setRegexPattern("^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

        textField.setValidators(valid);
        textField.getValidators().get(0).setMessage("Email is not valid!");

    }

    private void doEmailValidate(){
        emailValidator = new EmailValidator();
        textField.focusedProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
                if (!newPropertyValue) {
                    boolean val = textField.validate();

                }
            }
        });

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane bp = new BorderPane();
        bp.setPadding(new Insets(10,10,10,10));
        bp.setStyle("-fx-background-color: #fff");
        Label label = new Label("Your Email");
        JFXTextField jfxTextField = new JFXTextField();
        //call the email validator
        emailBox(jfxTextField);
        VBox vBox = new VBox();
        vBox.setSpacing(30);

        JFXButton jfxButton = new JFXButton("Submit");
        jfxButton.setStyle(" -fx-background-color: green;-fx-text-fill: white");

        vBox.getChildren().addAll(label, jfxTextField, jfxButton);
        bp.setCenter(vBox);

        Scene scene = new Scene(bp,400,600);
        primaryStage.setTitle("Email validation in JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Not pretty much i can say, what we are doing is just calling the method 'emailBox' and passed the JFXTextfield object that we need to have valid email on it.

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously