Completion of mandatory fields

Asked

Viewed 407 times

3

How do I force at least one of the two fields to be mandatory (is filled)? For example, the fields Celular and Telefone cannot be saved empty, only one of them.

My code:

//..
}
else ((txtNome.Text == "") || (maskedCPF.Text == "") || (maskedCEP.Text == "") || (txtNum.Text == "") || (maskedCelular.Text == "") || (txtTelefone.Text == ""))     
{
  MessageBox.Show("Os Campos com * são de Preenchimentos Obrigatórios!");
}

In this case it is forcing the two fields (Cellular and Telephone) to be filled. I want it to be possible to save with the filled cell field and the empty phone field or vice versa.

2 answers

4


It would be something like that:

else ((txtNome.Text == "") || (maskedCPF.Text == "") || (maskedCEP.Text == "") ||
    (txtNum.Text == "") || ((maskedCelular.Text == "") && (txtTelefone.Text == "")))

Using the operator of and (&&) only if the two are empty is there a problem.

Or, depending on the case:

if ((txtNome.Text != "") && (maskedCPF.Text != "") && (maskedCEP.Text != "") &&
    (txtNum.Text != "") && ((maskedCelular.Text != "") || (txtTelefone.Text != "")))

I put in the Github for future reference.

  • That’s right.vlw.

  • @Take a look at the [tour] besides accepting a reply you can vote on everything you find interesting in the whole site.

  • @Maniero why conditions are between parentheses?

  • @Mateusdaniel Probably because the person wants to make more explicit, I would not.

  • @Maniero ops, sorry, I thought that was part of your solution'

0

I got it a different way, it was like this:

protected void Page_Load(object sender, EventArgs e)
{
 if(!IsPostBack)
 {
   txtNome.Attributes["required"] = "true";
   masked.Attributes["required"] = "true";
 }
}

Browser other questions tagged

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