c# – How to Validate MaskedTextbox?

Question:

I have a record field which mixes textbox common with maskedtextbox , I created a validation code that does not allow the user to complete the registration if one of them are vazias.Mas is only validating the textbox common and not the maskedtextbox , allowing complete the registration form with fields important being empty. Here is the code that explains how I 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 the maskedtextbox as the normal textbox because I had already programmed a good part of the application when I replaced it with maskedtextbox . The fields that use maskedtextbox are the CPF, RG, Cellular and Landline (phone) fields.

Answer:

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 elements.

I saw it here: https://social.msdn.microsoft.com/Forums/pt-BR/afd4a25d-5c29-4218-8d12-df03f9741117/como-validar-maskedtextbox?forum=vscsharppt

Scroll to Top