Validate jFormattedTextField with Mascara

Asked

Viewed 796 times

1

I’m using jFormattedTextField with Mascara but I don’t know how to validate if the whole field has been filled.

jFormattedTextField

        // CPF
    JLabel lblCpf = new JLabel("CPF");
    contentPane.add(lblCpf, "cell 0 10");

    JFormattedTextField ftCPF = new JFormattedTextField();
    MaskFormatter mfCPF = new MaskFormatter();
    try {
        mfCPF.setMask("###.###.###-##");
        mfCPF.install(ftCPF);
        ftCPF.setText("");
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    contentPane.add(ftCPF, "cell 0 11,growx");

Button: Okay

// Botão > Ok
    JButton btnOk = new JButton("Ok");
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        // Validar Campos
            if(tfNome.getText().length() == 0) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Nome", "Informação", JOptionPane.INFORMATION_MESSAGE);
                tfNome.requestFocusInWindow();
            } else if(pfSenha.getPassword().length == 0) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Senha", "Informação", JOptionPane.INFORMATION_MESSAGE);
                pfSenha.requestFocusInWindow();
            } else if(pfCSenha.getPassword().length == 0) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Confirmação de Senha", "Informação", JOptionPane.INFORMATION_MESSAGE);
                pfCSenha.requestFocusInWindow();
            } else if(cbGrupo.getSelectedItem().equals("")) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Grupo.", "Informação", JOptionPane.INFORMATION_MESSAGE);
                cbGrupo.requestFocusInWindow();
            } else if(cbEstado.getSelectedItem().equals("")) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Estado", "Informação", JOptionPane.INFORMATION_MESSAGE);
                cbEstado.requestFocusInWindow();
            } else if(ftCPF.getText().equals(null)) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo CPF", "Informação", JOptionPane.INFORMATION_MESSAGE);
                ftCPF.requestFocusInWindow();
            } else if(cbPergunta.getSelectedItem().equals("")) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Pergunta Secreta", "Informação", JOptionPane.INFORMATION_MESSAGE);
                cbPergunta.requestFocusInWindow();
            } else if(pfResposta.getPassword().length == 0) {
                JOptionPane.showMessageDialog(null, "Por favor, informar campo Resposta Secreta", "Informação", JOptionPane.INFORMATION_MESSAGE);
                pfResposta.requestFocusInWindow();
            }

1 answer

2


else if(ftCPF.getText().trim().length() < 14) { JOptionPane.showMessageDialog(null, "Por favor, informar campo CPF", "Informação", JOptionPane.INFORMATION_MESSAGE); ftCPF.requestFocusInWindow();

Try this, if the string size that stores Cpf is different from 14(which is the digit number + the characters in the mask) then Cpf is not valid.

  • solved with Trim() and < 14 digits. Thanks champion!!

  • Good, I just don’t understand why Trim(), if I remember it takes out the spaces that are before or after the string, correct?

  • Correct, I didn’t understand it either more he only understands it after I played Tim(). Very strange this. Thank you!!

Browser other questions tagged

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