如何在C#中为电子邮件地址创建数据验证

时间:2017-03-17 10:04:32

标签: c# visual-studio validation

下面是通过输入框将数据插入我的表格的代码,我已经使用了一些数据验证,但想知道如何验证电子邮件地址输入框是否为电子邮件格式(@。)

    private void ECVSavebutton(object sender, RoutedEventArgs e)
    {

        string EmployeeAvailability;
        if (Job1DropDownBox.Text == "True" && Job2DropDownBox.Text == "True" && Job3DropDownBox.Text == "True")

        {
            EmployeeAvailability = "False";

        } else
        {
            EmployeeAvailability = "True";
        }
        {
            if (PostcodeInputBox.Text.Count() >= 7)
                PostcodeInputBox.Text = null;
        }
        Connection.Open();
        SqlCommand Command = new SqlCommand(null, Connection);
        Command.CommandText = "Insert into [StaffDetails] (Forename,Surname,DateofBirth,Emailaddress,Country,Address,City,Postcode,Skill1,Skill2,Skill3,Job1,Job2,Job3,EmployeeAvailability,Location) Values(@forename, @surname, @dateofbirth, @emailaddress, @country, @address, @city, @postcode, @skill1, @skill2, @skill3, @job1, @job2, @job3, @employeeavailability,@location)";
        Command.Parameters.AddWithValue("@forename", ForenameInputBox.Text);
        Command.Parameters.AddWithValue("@surname", SurnameInputBox.Text);
        Command.Parameters.AddWithValue("@dateofbirth", DateOfBirthPicker.SelectedDate);
        Command.Parameters.AddWithValue("@emailaddress", EmailInputBox.Text);
        Command.Parameters.AddWithValue("@country", CountryInputBox.Text);
        Command.Parameters.AddWithValue("@address", AddressInputBox.Text);
        Command.Parameters.AddWithValue("@city", CityInputBox.Text);
        Command.Parameters.AddWithValue("@postcode", PostcodeInputBox.Text);
        Command.Parameters.AddWithValue("@skill1", Skill1DropDownBox.Text);
        Command.Parameters.AddWithValue("@skill2", Skill2DropDownBox.Text);
        Command.Parameters.AddWithValue("@skill3", Skill3DropDownBox.Text);
        Command.Parameters.AddWithValue("@job1", Job1DropDownBox.Text);
        Command.Parameters.AddWithValue("@job2", Job2DropDownBox.Text);
        Command.Parameters.AddWithValue("@job3", Job3DropDownBox.Text);
        Command.Parameters.AddWithValue("@employeeavailability", EmployeeAvailability);
        Command.Parameters.AddWithValue("@location", LocationDropDownBox.Text);
        Command.ExecuteNonQuery();
        Connection.Close();

1 个答案:

答案 0 :(得分:-1)

您可以查看以下链接.. 我认为这是验证电子邮件的绝佳方式之一.. 使用try..catch块来获取布尔值.. https://stackoverflow.com/a/1374644/5518217