How to Validate Maskedtextbox?

Asked

Viewed 1,744 times

3

I have a record field that mixes textbox common with maskedtextbox, created a validation code that does not allow the user to register if one of them is empty. But this only validates the textbox common and not the maskedtextbox, allowing registration with important fields being empty. Follow 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 textbox because I had already programmed much of the application when I replaced it with maskedtextbox. The fields they use maskedtextbox are the fields of CPF, RG, Cellular and Fixed(phone).

1 answer

3

  • Thanks, I’m doing a college job and I wasn’t getting it, you saved me.

Browser other questions tagged

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