How to Validate MaskedTextbox?

2

I have a registration field which mixes textbox common with maskedtextbox , I created a validation code that does not allow the user to register if one of them is empty. But he is only validating the textbox common and not% the maskedtextbox , allowing to register with important fields being empty. Follow the code that explains how to do this process.

private void btnInserir_Click(object sender, EventArgs e)
    {
        ConsultaPacientes.msg = true;
        if (cbxConvenio.SelectedItem == null)
        {
            MessageBox.Show("O campo PAGAMENTO deve ser preenchido!");
            cbxConvenio.Focus();
            cbxConvenio.BackColor = Color.LightSeaGreen;
            cbxConvenio.DroppedDown = true;
        }
        else if(txtNome.Text == string.Empty)
        {
            MessageBox.Show("O campo NOME deve ser preenchido!");
            txtNome.BackColor = Color.LightSeaGreen;
        }
        else if (String.IsNullOrWhiteSpace(txtCPF.Text))
        {
            MessageBox.Show("O campo CPF deve ser preenchido!");
            txtCPF.BackColor = Color.LightSeaGreen;
        }
        else if (txtRG.Text == "")
        {
            MessageBox.Show("O campo RG deve ser preenchido!");
            txtRG.BackColor = Color.LightSeaGreen;
        }
        else if (txtCidade.Text == string.Empty)
        {
            MessageBox.Show("O campo CIDADE deve ser preenchido!");
            txtCidade.BackColor = Color.LightSeaGreen;
        }
        else if (txtRua.Text == string.Empty)
        {
            MessageBox.Show("O campo RUA deve ser preenchido!");
            txtRua.BackColor = Color.LightSeaGreen;
        }
        else if(txtCelular.Text == "" || txtFixo.Text == "")
        {
            MessageBox.Show("Um dos campos de telefone deve ser preenchido!");
            txtCelular.BackColor = Color.LightSeaGreen;
            txtFixo.BackColor = Color.LightSeaGreen;
        }
        else
        {
            if (btnInserir.Text == "Inserir")
            {
                btnInserir.Tag = "Inserir Novo Paciente";
                Pacientes_TCCTableAdapter TaPaciente = new Pacientes_TCCTableAdapter();
                TaPaciente.Insert(txtNome.Text, txtCPF.Text, txtRG.Text, cbxSexo.Text, dtpData.Value, txtCidade.Text, txtEstado.Text, txtBairro.Text, txtCEP.Text,
                txtRua.Text, txtNumero.Text, txtCelular.Text, txtFixo.Text, txtEmail.Text, cbxConvenio.Text);
                Close();

                if (MessageBox.Show("Paciente gravado com o ID: " + TaPaciente.UltimooID() + " deseja fazer outro cadastro?", "Confirma", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                {
                    Close();
                }
                else
                {
                    FrmCadCliente novo = new FrmCadCliente();
                    novo.Show();
                }
            }


            else
            {
                Pacientes_TCCTableAdapter TaPaciente = new Pacientes_TCCTableAdapter();
                TaPaciente.Update(txtNome.Text, txtCPF.Text, txtRG.Text, cbxSexo.Text, dtpData.Value, txtCidade.Text, txtEstado.Text,
                txtBairro.Text, txtCEP.Text, txtRua.Text, txtNumero.Text, txtCelular.Text, txtFixo.Text, txtEmail.Text, cbxConvenio.Text, (int.Parse(txtID.Text)));
                MessageBox.Show("Paciente editado com sucesso!");
                Close();
            }
        }
    }

I ended up naming maskedtextbox as textbox normal because I had already programmed a good part of the application when I made the substitution with maskedtextbox . The fields that use maskedtextbox are the fields of CPF, RG, Cellular and Fixed (telephone).

    
asked by anonymous 13.11.2016 / 17:01

1 answer

2

I solved my problem using:

else if (!txtCPF.MaskCompleted) 
    {
        MessageBox.Show("O campo CPF deve ser preenchido!");
        txtCPF.BackColor = Color.LightSeaGreen;
    }

I learned that I can use both MaskCompleted and MaskFull The difference is that MaskCompleted validates the mandatory elements of the mask and MaskFull validates both mandatory and optional.

I saw it here: link

    
13.11.2016 / 20:28