C# 检查同一记录中的 2 个字段是否与用户输入的内容匹配,例如用户名和密码密码

发布于 2024-10-25 20:43:38 字数 443 浏览 5 评论 0原文

我想做一个登录类型表单,让员工输入他们的 EmployeeID 和 DOB。到目前为止,我的代码是检查两个文本框是否不为空,确保 EmployeeID 存在,但我不确定如何检查员工的 DOB 是否与他们输入的相同。一些代码如下。

if ((txtEmployeeID.TextLength != 0) && (txtDOB.TextLength != 0))
{

      employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

      if (employeesBindingSource.Count > 0)
      {

           // DOES DOB FOR EMPLOYEE MATCH - NOT SURE WHAT TO PUT HERE

      }
}

I want to do a login type form where the Employee enters their EmployeeID and DOB. The code I have so far is to check that both text boxes aren't blank, make sure the EmployeeID exists, but i'm not sure how to check that the DOB for the Employee is the same as what has been entered by them. Some code is below.

if ((txtEmployeeID.TextLength != 0) && (txtDOB.TextLength != 0))
{

      employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

      if (employeesBindingSource.Count > 0)
      {

           // DOES DOB FOR EMPLOYEE MATCH - NOT SURE WHAT TO PUT HERE

      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意犹 2024-11-01 20:43:38

为此,我们需要 Employee DOB 变量 - 将 DataOfBirthVariable 替换为该变量。

if ((txtEmployeeID.TextLength != 0) && (txtDOB.TextLength != 0))
{

      employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

      if (employeesBindingSource.Count > 0 && DataOfBirthVariable == txtDob)
      {


      }
}

另外,最佳实践是使用 !String.IsNullOrEmpty(txtEmployeeID) 而不是 .TextLength - 只是一个提示。

For this we're going to need the Employee DOB variable - replace DataOfBirthVariable with this.

if ((txtEmployeeID.TextLength != 0) && (txtDOB.TextLength != 0))
{

      employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";

      if (employeesBindingSource.Count > 0 && DataOfBirthVariable == txtDob)
      {


      }
}

Also, it's best practice to use !String.IsNullOrEmpty(txtEmployeeID) instead of .TextLength - just a tip.

镜花水月 2024-11-01 20:43:38

假设您尝试匹配的 DoB 是 DateTime

DateTime enteredDoB;
bool matchedDoB;

if (DateTime.TryParse(txtDOB, out enteredDoB))
{
    matchedDoB = employeeDoB.Equals(enteredDoB);
}

请参阅 DateTime.TryParse

Assuming the DoB you trying to match with is a DateTime

DateTime enteredDoB;
bool matchedDoB;

if (DateTime.TryParse(txtDOB, out enteredDoB))
{
    matchedDoB = employeeDoB.Equals(enteredDoB);
}

See DateTime.TryParse

七月上 2024-11-01 20:43:38

日期的问题是让您的员工以正确的格式输入数据。

  • Jan 1, 1970
  • 1/1/1970
  • 1/1/70
  • 1-1-70
  • January 01, '70

同一事物的所有变体。

您将不得不强制您的员工以您需要的格式输入日期字段。

static char DATESPLITTER = '/'; // define this accordingly
static DateTime NODATE = new DateTime(1, 1, 1900);

private DateTime GetDate(string dateValue) {
  if (!String.IsNullOrEmpty(dateValue) {
    string[] split = dateValue.Split(DATESPLITTER);
    if (split.Length == 3) {
      int m = Convert.ToInt32(split[0]);
      if ((0 < m) && (m < 13)) {
        int d = Convert.ToInt32(split[1]);
        if ((0 < d) && (d < 32)) {
          int y = Convert.ToInt32(split[2]);
          if ((0 < y) && (y < DateTime.Now.Year)) {
            if (y < 100) y += 2000;
            return new DateTime(m, d, y);
          }
        }
      }
    }
  }
  return NODATE;
}

这与我在代码中使用的一个小路由非常相似。

The problem with a date is getting your employees to input the data in the correct format.

  • Jan 1, 1970
  • 1/1/1970
  • 1/1/70
  • 1-1-70
  • January 01, '70

All variations of the same thing.

You are going to have to force your employees to input the date field in a format you require.

static char DATESPLITTER = '/'; // define this accordingly
static DateTime NODATE = new DateTime(1, 1, 1900);

private DateTime GetDate(string dateValue) {
  if (!String.IsNullOrEmpty(dateValue) {
    string[] split = dateValue.Split(DATESPLITTER);
    if (split.Length == 3) {
      int m = Convert.ToInt32(split[0]);
      if ((0 < m) && (m < 13)) {
        int d = Convert.ToInt32(split[1]);
        if ((0 < d) && (d < 32)) {
          int y = Convert.ToInt32(split[2]);
          if ((0 < y) && (y < DateTime.Now.Year)) {
            if (y < 100) y += 2000;
            return new DateTime(m, d, y);
          }
        }
      }
    }
  }
  return NODATE;
}

That's very similar to a little routing I use in my code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文