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.


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:


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;

        RegexValidator valid = new RegexValidator();

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

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


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



    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
        VBox vBox = new VBox();

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

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

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

    public static void main(String[] args) {

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

STOP following those self-help content, START doing your immediate tasks and responsibilities

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

ASUS Battery Health Charging Software To Make Your Battery Life Longer