eclipsevalidationjavafxtexttextbox

Javafx eclipse Text Input Validate when numeric or Mixed and reject when Non-numeric


In JavaFx eclipse application, before saving the data in SQL data base, it is required to verify following conditions: Either Numeric Value or Mixed. But,reject if purely non-numeric with(out) special characters like JHGYyree. Allowed text: 1234, A1254, b64A8 (Ignore Case) Not Allowed: aasdrgf, ASDSA, Asxss, @#vdvd, @33KJJJ, #@878sasa.

The data entered is to be checked after pressing Enter key or Mouse click on Save Button. How to write such a function in JavaFX ?

     @FXML
     private Label lblOffName;

     Boolean static input_validation == false;
     String id = txtOffName.getText();

     if(validate_text(id) == true){

    }
    public Boolean validate_text(String str){
    // validation code here ...
   return input_validation;
   }

Solution

  • Example

    An example solution using a loop (assumes a basic English character set). You could use regex instead, but I leave that as an exercise for the reader.

    package application;
    
    public class InputValidator {
    
        public static void main(String[] args) {
            String[] testData = {
                    // valid 
                    "1234", "A1254", "b64A8",
                    // invalid
                    "aasdrgf", "ASDSA", "Asxss", "@#vdvd", "@33KJJJ", "#@878sasa"
            };
    
            for (String input: testData) {
                validate(input);
            }
        }
    
        /**
         * Either Numeric Value like or Mixed. 
         * But, to reject if purely non-numeric with(out) 
         * special characters like JHGYyree.
         *
         * @param input string to validate
         * @return true if the input string is value.
         */
        private static boolean validate(String input) {
            boolean hasDigit = false;
            boolean hasAlpha = false;
            boolean hasSpecial = false;
    
            for (int i = 0; i < input.length(); i++) {
                char c = Character.toLowerCase(input.charAt(i));
    
                if (isDigit(c)) {
                    hasDigit = true;
                }
    
                if (isAlpha(c)) {
                    hasAlpha = true;
                }
    
                if (!isDigit(c) && !isAlpha(c)) {
                    hasSpecial = true;
                }
            }
    
            boolean isValid =
                    (hasDigit && !hasAlpha && !hasSpecial) || (hasDigit && hasAlpha && !hasSpecial);
    
            System.out.println(input + " > " + isValid);
    
            return isValid;
        }
    
        private static boolean isDigit(char c) {
            return '0' <= c && c <= '9';
        }
    
        private static boolean isAlpha(char c) {
            return 'a' <= c && c <= 'z';
        }
    }
    

    Output

    1234 > true
    A1254 > true
    b64A8 > true
    aasdrgf > false
    ASDSA > false
    Asxss > false
    @#vdvd > false
    @33KJJJ > false
    #@878sasa > false
    

    Integration with JavaFX

    I will also leave this as an exercise for the reader.

    Either you can write the integration code yourself, or leverage a validation framework such as: