I want to either show an alert dialog box or disable the submit button if the length of the phone number is less than 11.
Here is the code for the formatted text field where the user enters the number:
numText = new JFormattedTextField(createFormatter("#### #######"));
numText.setColumns(15);
numLabel = new JLabel("Enter Phone Number: ");
enterButtonTemp = new JButton("Enter");
The code to check the condition is:
phoneStr = numText.getText();
System.out.println(phoneStr.length());
if(phoneStr.length() <= 11){
//show the JoptionPane for dialog box
}
else{
//Send the name to be stored in db
}
The code basically checks if the length of the phoneStr is less than 11. If it is it should pop up a dialog box but the .length() method gives 12 as an output even if the text field is left empty. The following statement outputs 12. Is there any other way to check this condition.
System.out.println(phoneStr.length());
Found a solution. Not that efficient but a bit quicker. The formatted text field actually creates the length of the field 12 by default and thus if no input is entered, 12 spaces are inserted by default. So instead of using phoneStr.equals(""), the following code works well. It compares phoneStr with 12 spaces instead of an empty string.
if(phoneStr.equals(" ")){
System.out.println("in loop");
JOptionPane.showMessageDialog(dialogFrame,"Fill all the fields.","Alert",JOptionPane.WARNING_MESSAGE);
}
else{
//Send the num to db
}