Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to validate a textbox to accept user input in certain format
I need user input as first 3 digits as number 123 followed by year ranging 1950-current year and followed by six digit unique identifier.

I am not much familiar with this kind of validations.

Any suggestions and help is appreciated.
Posted
Updated 5-Nov-15 14:19pm
v2

 
Share this answer
 
Just validate it, if you need. What's the problem?

But better idea is just not to allow the user to enter wrong data, unless the rules are too confusing. For the time-relate data, however, it would be much better to use some kind of "DateTimePicker". Unfortunately you did not inform us what UI framework/library you are using. It can be a .NET FCL control, such as System.Windows.Forms.DateTimePicker, or some 3rd-party control. Always, always tag the UI framework/library you are using.

—SA
 
Share this answer
 
Comments
Member 12076824 5-Nov-15 21:16pm    
Thanks for reply, I am trying to use key press event here, as user enter 123 next 4 digits needs to be year ranging 1950-2015 and 6 digits.

I am trying to use array and divide that into parts but that was not working.
Sergey Alexandrovich Kryukov 5-Nov-15 21:55pm    
I tried to explain why it's bad. But anyway, if you need to do it this way, just validate.
—SA
Member 12076824 5-Nov-15 22:04pm    
Thanks I missed that part. As per the requirement I had to opt for validation unfortunately.
Sergey Alexandrovich Kryukov 5-Nov-15 22:50pm    
Any problems with that? Here is what I tend to advice: don't make it hard, don't try to validate on each step, say, when it looses focus, on each character input or the like. Validate everything just before you are about using the data, say, before some kind of "submit".
If you are clear about it, will you accept the answer formally? If you have further question, they will be welcome anyway.
—SA
Updated:
C#
bool IsMillennium = false;
 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string CurrentYear = DateTime.Now.Year.ToString();

            if (e.KeyChar == 8)
            {
                e.Handled = false;  // Accept BackSpace
                return;
            }

            if (!char.IsNumber(e.KeyChar))
            {
                e.Handled = true;  // reject any key press but numbers
                return;
            }

            if (textBox1.TextLength >= 4)  // to know which Millennium wuser enters
            {
                if (textBox1.Text[3] == '1')  // [1]950
                {
                    IsMillennium = false; // this mean we at 1950 ~ 1999
                }
                else if (textBox1.Text[3] == CurrentYear[0]) //Part b
                {
                    IsMillennium = true;   //this mean we at 2000 till current year (AKA 2015)
                }
            }

            if (textBox1.TextLength < 13)   // Make sure we at our text limits
            {
                if (textBox1.TextLength < 3) // for first 3 chars we accept Number only
                {
                    if (char.IsNumber(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
                else
                {
                    // if the text lenght=3 this mean we enter the year part valdiation
                    // year valdiation have 4 partsto valdiate
                    // [1][9][5][0] till CurrentYear[0][1][2][3] variable

                    if (textBox1.TextLength == 3)
                    {
                        //year Part 1
                        // need to accept date between 1950 ~ Current Year
                        //so we check first year char either 1 for [1]950 and  CurrentYear[0] which is first char in our variable
                        // so it must be between 1 and CurrentYear[0] that's why we convert it to Int
                        var yearchar1 = int.Parse(e.KeyChar.ToString());
                        //if ((yearchar1 == 1 || yearchar1 <= int.Parse(CurrentYear[0].ToString())) && yearchar1 != 0)
                        if (yearchar1 <= int.Parse(CurrentYear[0].ToString()) && yearchar1 != 0)
                        {
                            e.Handled = false;
                        }
                        else
                        {
                            e.Handled = true;
                        }
                    }
                    else if (textBox1.TextLength == 4)
                    {
                        //year Part 2
                        //this part is tricky we can't compare it betwwen 9 and 0 casue this will give us all numbers range 0~9 and that's we don't need 
                        //but wee need to break it into 2 parts
                        //Part A: if last char was 1 then we need 9 to follow it for :"1[9]50"
                        //Part B: if last char was CurrentYear[0] then our key need to be smaller than or equal of CurrentYear[1]

                        var yearchar2 = int.Parse(e.KeyChar.ToString());
                        if (textBox1.Text[3] == '1')  // Part A
                        {
                            if (yearchar2 == 9)
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                e.Handled = true;
                            }
                        }
                        else if (textBox1.Text[3] == CurrentYear[0]) //Part B
                        {
                            if (yearchar2 <= int.Parse(CurrentYear[1].ToString()))  //key need to be smaller than or equal of CurrentYear[1]
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                e.Handled = true;
                            }
                        }
                    }
                    else if (textBox1.TextLength == 5)
                    {
                        //year Part 3
                        //like last part we need to know where we are to do our validation right.
                        // thankfully we got our variable IsMillennium which we set it earlier
                        // and we can use it only after  when there is 4 or more chars in our textbox otherwise it's value will be wrong
                        //IsMillennium = false mean we validate 19[5]0 ~ 19[9]9
                        //IsMillennium = true mean we validate 20[0]0 ~ CurrentYear AKA 20[1]5
                        // Part C: betwwen 5 and 9
                        // Part D: key smaller than or equal CurrentYear[2]
                        var yearchar3 = int.Parse(e.KeyChar.ToString());
                        if (!IsMillennium)
                        {
                            if (yearchar3 >= 5 && yearchar3 <= 9) // Part C
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                e.Handled = true;
                            }
                        }
                        else
                        {
                            if (yearchar3 <= int.Parse(CurrentYear[2].ToString()))  // part D
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                e.Handled = true;
                            }
                        }

                    }
                    else if (textBox1.TextLength == 6)
                    {
                        // year part 4
                        // like other we break our problem to smaller problems so it be easyer to solve
                        // Part E: If IsMillennium = false then it must between 0 and 9   ==== 195[0]
                        // Part F: If IsMillennium = true then it must be smaller than or equal CurrentYear[3] ==== 201[5]
                        // another Trick here Millennium will consist of 2 or more parts
                        // 1st part for 2000~2009  and 2nd part for 2010 to current year
                        // Part I: we can break this more to be valide for ever by check if textBox1.Text[5] <  int.Parse(CurrentYear[2].ToString()) then we accept all number ranges
                        // Part II: but if it equal to our current then we accept
                        var yearchar4 = int.Parse(e.KeyChar.ToString());
                        if (!IsMillennium)
                        {
                            if (yearchar4 >= 0 && yearchar4 <= 9) // Part E
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                e.Handled = true;
                            }
                        }
                        else
                        {    // Part F
                            if (int.Parse(textBox1.Text[5].ToString()) < int.Parse(CurrentYear[2].ToString()))// Part I
                            {
                                e.Handled = false;
                            }
                            else
                            {
                                if (yearchar4 <= int.Parse(CurrentYear[3].ToString()))  // Part II
                                {
                                    e.Handled = false;
                                }
                                else
                                {
                                    e.Handled = true;
                                }
                            }

                        }

                    }
                    else if (textBox1.TextLength > 6)
                    {
                        //TO DO : Six digit unique identifier.
                        if (char.IsNumber(e.KeyChar))
                        {
                            e.Handled = false;
                        }
                        else
                        {
                            e.Handled = true;
                        }
                    }
                }
            }
            else
            {
                // reject any key press after it reach max valid lenght
                e.Handled = true;
            }
        }


Edit :
well, if you done validation on key press then why you need validation on leave you already made it hard to type keys and key press event accept specific keys only.
you can validate on leave if the text have the correct length or not in your case something like that
C#
private void textBox1_Leave(object sender, EventArgs e)
        {
            if(textBox1.TextLength != 10)
            {
                textBox1.Focus();
            }
        }

Please Notice that will make it so hard to user because it will prevent him from leave the textbox until he enter the correct format.
 
Share this answer
 
v8
Comments
Member 12076824 6-Nov-15 11:15am    
Thanks for response, that's something I was looking for.

I will check this out, and ask you if I have any questions.
tornadofay 6-Nov-15 14:52pm    
no problem, for this type of validations you better write it first on paper in steps for what you really need. if you done it right on paper then it will be so easy to implement with code.
Member 12076824 6-Nov-15 15:15pm    
I was also trying to implement same as user enters input and leaves the textbox, as if event is fired at textbox1_leave to verify user input with required format, can you please help me with that?
Member 12076824 6-Nov-15 16:26pm    
I agree I tried the leave part as you mentioned it works fine to verify length of textbox, but won't be of much help in my case. I will work with keypress event.

Thanks
Member 12076824 6-Nov-15 20:13pm    
Is there any way to make sure it's validated till current year and at the same time compatible with next year and so on?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900