Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Is there a better way to validate user input [closed]

I have the following code:

private void btnOK_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(tbVendorName.Text))
        {
            VendorName = tbVendorName.Text;
            if (!string.IsNullOrEmpty(rtbVendorAddress.Text))
            {
                VendorAddress = rtbVendorAddress.Text;
                if (!string.IsNullOrEmpty(tbVendorEmail.Text))
                {
                    VendorEmail = tbVendorEmail.Text;
                    if (!string.IsNullOrEmpty(tbVendorWebsite.Text))
                    {
                        VendorWebsite = tbVendorWebsite.Text;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Vendor Website Required");
                    }
                }
                else
                {
                    MessageBox.Show("Vendor email is required");
                }
            }
            else
            {
                MessageBox.Show("Vendor address is required");
            }
        }
        else
        {
            MessageBox.Show("Vendor name is required");
        }
    }

But it just looks horrible. Is there a better way? Or even an alternative methodology which makes the code more readable?

like image 739
Dave Gordon Avatar asked Sep 02 '13 07:09

Dave Gordon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C language?

C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

The better way is to master the MVVM pattern. There you could create a ViewModel and define all input data there:

class VendorViewModel
{
    [Required]
    public string Name { get; set; }

    public string Website { get; set; }

    [Regex("regex for email")]
    public string Email { get; set; }

    [MaxLength(160)]
    public string Address { get; set; }
}

Then the framework will show input errors (if any) after each textbox or any other input element. Enable property of the button will be automatically set according of all fields validated correctly.

like image 98
AgentFire Avatar answered Oct 20 '22 11:10

AgentFire


Put it in a method and return from the method a boolean as to valid or not.

 private void btnOK_Click(object sender, EventArgs e)
{
    if(!Validate())
    {
        //Invalid
    }
    //Valid so set details
}

private bool Validate()
{
     if (!string.IsNullOrEmpty(tbVendorName.Text))
     {
          MessageBox.Show(...);
        return false;
     }

    return true;
}
like image 3
Sayse Avatar answered Oct 20 '22 09:10

Sayse