Invalidate characters from a Maskformatter

Asked

Viewed 115 times

0

So guys, I have a window where I need to go through a validation of null fields of several JTextField and a JFormattedTextField, where the latter has a mask for phone numbers "(##)####-####". The fact is that even though I don’t type anything in this field, it takes the characters from the mask : ()- and understands that the field is not null. I wonder if there’s some method that ignores characters from a mask, or I’ll have to do all that work with substring ? It is worth making clear that I just want to ignore at the time of validation, and not remove these characters, because then I will have to introduce them to the user !

As a matter of fact :

try {
        mskTel = new MaskFormatter("(##)####-####");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    txtTel = new JFormattedTextField(mskTel);

How the validation is done :

form.txtTel.getText() == null || form.txtTel.getText().trim().isEmpty()
  • How are you picking up the values of this field? Add a [mcve] so we can analyze the problem.

  • 1

    A "nut" solution would be to check the size of the returned string, its field should have 13 characters, so form.txtTel.getText() = form.txtTel.getText().trim().length == 13 ? form.txtTel.getText().trim() : null.

  • when I apply this solution gives compilation error : "The left-hand side of an assignment must be a variable". So if I try to assign the form.txtTel.getText() is a String, it accuses another error, and suggests that I add a () after the .length, and from there, the validation no longer works ...

  • It was bad, my mistake. The correct thing is to assign a string variable: String text = form.txtTel.getText().trim().length == 13 ? form.txtTel.getText().trim() : null

  • But this command I put before validation, or replace the corresponding chunk ?

  • Where do you do this validation? Num if?

  • Exactly ! See code excerpt : http://pastebin.com/krFV37Vc

  • Simply replace the validation of Empty: http://pastebin.com/H04xkm9Z

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.