Field validation

Asked

Viewed 106 times

-1

How could I validate the fields below so they don’t go blank, because my biggest difficulty is that two of them have Convert.

NegocioFuncionario neg = new NegocioFuncionario();
EntidadeFuncionario ent = new EntidadeFuncionario();
ent.Matricula = Convert.ToInt32(txtMatricula.Text);
ent.DataNascimento = Convert.ToDateTime(txtNascimento.Text);
ent.Nome  = txtNome.Text;
ent.Situacao = cboSituacao.SelectedItem.ToString();
  • Tag the language you are using.

  • I don’t know which link you’re using, but I believe a simple if will solve your problem, e.g.: if(txtMatricula.Text == "" || txtMatricula.Text == null){ Return false; }

  • I believe you are working with C#, if you are, you can do it this way: if (txtMatricula.Text == string.Empty){ Return false; }

  • I’m using the C# anyway. However, when including this command always gives this error message "System.Formatexception: 'Input string was not in a correct format", because it txtMatricula field is being converted to string at first.

  • But at what point you entered the command if?? It would have to be before the conversion, then you won’t have a problem.

  • This, before the conversion, I created a response with the code.

  • You’re a genius, Wictor. My problem has been solved with something very simple and practical. Thanks a lot!

Show 2 more comments

1 answer

2

You can do the check this way:

if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) {
    // Mensagem para o usuário
    return false;
}

Using your own code:

if (string.IsNullOrWhiteSpace(txtMatricula.Text)) {
    // Mensagem para o usuário
    return false;
}

if (string.IsNullOrWhiteSpace(txtNascimento.Text)) {
    // Mensagem para o usuário
    return false;
}

NegocioFuncionario neg = new NegocioFuncionario();
EntidadeFuncionario ent = new EntidadeFuncionario();
ent.Matricula = Convert.ToInt32(txtMatricula.Text);
ent.DataNascimento = Convert.ToDateTime(txtNascimento.Text);
ent.Nome  = txtNome.Text;
ent.Situacao = cboSituacao.SelectedItem.ToString();

Source: https://stackoverflow.com/questions/6156458/check-if-textbox-is-empty-and-return-messagebox

Browser other questions tagged

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