Question How can I validate two different ID number with the same textbox?

Ajhezort

New member
Joined
Jan 10, 2022
Messages
2
Programming Experience
Beginner
Here's the code for the textbox where the ID number is being validated. The textbox with red circle.
Sample ID Number:
For Teacher: 123-1000
For Student; 123-100000

The problem is when I input the ID number of the student, it will read the ID of the teacher first which they have both the same start of the ID number of the student.
I don't know what is the best thing to do.
1641799017120.png


I just based it from the length of the two ID's


Code:
C#:
private void textBox7_TextChanged(object sender, EventArgs e)
        {
            if (textBox7.TextLength >= 8)
            {
                stloadpicture();
                loadpicture();
                studname();
                employeename();

                if (textBox1.Text != "")
                {
                    if (textBox8.Text == "1")
                    {
                        timelog = DateTime.Now.ToString("HH:mm");
                        daylog = DateTime.Now.ToString("dd");
                        monthlog = DateTime.Now.ToString("MMMM");
                        dayweeklog = DateTime.Today.DayOfWeek.ToString();
                        textBox2.Text = timelog;
                        textBox4.Text = daylog;
                        textBox5.Text = monthlog;
                        textBox6.Text = dayweeklog;
                        logssiya();
                        stloadpicture();


                        if (textBox3.Text == "Time-In")
                        {
                            if (dataGridView1.Rows.Count > 0)
                            {
                                if (dataGridView1.CurrentRow.Cells[6].Value.ToString() == "x")
                                {
                                    textBox9.Text = "You have already timed-in!";
                                }
                                else
                                {
                                    if (dataGridView1.CurrentRow.Cells[6].Value.ToString() == timelog && dataGridView1.CurrentRow.Cells[2].Value.ToString() == monthlog && dataGridView1.CurrentRow.Cells[3].Value.ToString() == daylog)
                                    {
                                        textBox9.Text = "Time-In: Please wait a minute!";
                                    }
                                    else
                                    {
                                        op.studtimelogs(timelog, monthlog, daylog, dayweeklog, textBox7.Text, DateTime.Now.Year.ToString());
                                        textBox9.Text = "Thank You!";
                                    }
                                }
                            }
                            else
                            {
                                op.studtimelogs(timelog, monthlog, daylog, dayweeklog, textBox7.Text, DateTime.Now.Year.ToString());
                                textBox9.Text = "Thank You!";
                            }
                        }
                        else
                        {
                                if (dataGridView1.Rows.Count > 0)
                                {
                                    if (dataGridView1.CurrentRow.Cells[6].Value.ToString() == "x")
                                    {
                                        if (dataGridView1.CurrentRow.Cells[1].Value.ToString() == timelog && dataGridView1.CurrentRow.Cells[2].Value.ToString() == monthlog && dataGridView1.CurrentRow.Cells[3].Value.ToString() == daylog)
                                        {
                                            textBox9.Text = "Time-Out: Please wait a minute!";
                                        }
                                        else
                                        {
                                            op.studtimelogs2(textBox2.Text, Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));
                                            textBox9.Text = "Thank You!";
                                        }
                                    }
                                    else
                                    {
                                        textBox9.Text = "Please! Time-in first!";
                                    }
                                }           
                        }
                        x = 0;
                        timer1.Start();
                    }


                    else if (textBox8.Text == "Active")
                    {
                        timelog = DateTime.Now.ToString("HH:mm");
                        daylog = DateTime.Now.ToString("dd");
                        monthlog = DateTime.Now.ToString("MMMM");
                        dayweeklog = DateTime.Today.DayOfWeek.ToString();
                        textBox2.Text = timelog;
                        textBox4.Text = daylog;
                        textBox5.Text = monthlog;
                        textBox6.Text = dayweeklog;
                        logsniya();
                        loadpicture();
                       
                        if (textBox3.Text == "Time-In")
                        {
                            if (dataGridView2.Rows.Count > 0)
                            {
                                if (dataGridView2.CurrentRow.Cells[6].Value.ToString() == "x")
                                {
                                    textBox9.Text = "You have already timed-in!";
                                }
                                else
                                {
                                    if (dataGridView2.CurrentRow.Cells[6].Value.ToString() == timelog && dataGridView2.CurrentRow.Cells[2].Value.ToString() == monthlog && dataGridView2.CurrentRow.Cells[3].Value.ToString() == daylog)
                                    {
                                        textBox9.Text = "Time-In: Please wait a minute!";
                                    }
                                    else
                                    {
                                        op.payrolltimelogs(timelog, monthlog, daylog, dayweeklog, textBox7.Text, DateTime.Now.Year.ToString());
                                        textBox9.Text = "Thank You!";
                                    }
                                }
                            }
                            else
                            {
                                op.payrolltimelogs(timelog, monthlog, daylog, dayweeklog, textBox7.Text, DateTime.Now.Year.ToString());
                                textBox9.Text = "Thank You!";
                            }
                        }
                        else
                        {
                            if (dataGridView2.Rows.Count > 0)
                            {
                                if (dataGridView2.CurrentRow.Cells[6].Value.ToString() == "x")
                                {
                                    if (dataGridView2.CurrentRow.Cells[1].Value.ToString() == timelog && dataGridView2.CurrentRow.Cells[2].Value.ToString() == monthlog && dataGridView2.CurrentRow.Cells[3].Value.ToString() == daylog)
                                    {
                                        textBox9.Text = "Time-Out: Please wait a minute!";
                                    }
                                    else
                                    {
                                        op.payrolltimelogs2(textBox2.Text, Convert.ToInt32(dataGridView2.CurrentRow.Cells[0].Value));
                                        textBox9.Text = "Thank You!";
                                    }
                                }
                                else
                                {
                                    textBox9.Text = "Please! Time-in first!";
                                }
                            }
                        }
                        x = 0;
                        timer1.Start();
                    }
                    else
                    {
                        textBox9.Text = "You account has been disabled!";
                        x = 0;
                        timer1.Start();
                    }
                }
                else
                {
                    textBox9.Text = "ID not found!";
                    x = 0;
                    timer1.Start();
                }
            }
        }
 
Last edited by a moderator:
If you're going to read and use the data when the Text property value changes then there's nothing you can do. The code can't magically know that the user hasn't finished typing. What you need to do is NOT read and use the data at that point. You need to wait until you actually know that the user has finished typing, which means requiring the user to perform some sort of action. The obvious choice is clicking a Button.
 
Back
Top Bottom