I am currently creating a registration form and it contains error handling processes for the text boxes, like certain characters are only allowed in a certain textbox and so on. So for my first try I tried creating an algorithm that will check if the user only enters letters from the keyboard meaning no numbers and no signs (excluding the period sign) so here is my take on this matter:

private bool isInCorrectInput = false;
private void tBoxTitle_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(isInCorrectInput))
            {
                tBoxTitle.SelectAll();
                errorProvider1.SetError(tBoxTitle, "This text box accepts letters and the '.' sign only as input, please try again.");
                e.Handled = true;
            }
        }

        private bool checkInputByKeyCode(Keys key)
        {
            if ((key >= Keys.Oem1) && (key <= Keys.Oem8) || ((key >= Keys.NumPad0) && (key <= Keys.NumPad9)) || ((key >= Keys.D0) && (key <= Keys.D9)) || (key == Keys.Divide) || (key == Keys.Multiply) || (key == Keys.Subtract) || (key == Keys.Add) || (key == Keys.Space))
            {
                return false;
            }

            else
            {
                return true;
            }
        }

YES i know it looks brute forced that's why I am asking you guys how could I optimize this code of mine here.

I don't know how to use regex. Although I know that regex will make my life easier. But its just that I came in late for our class and the only way I could make up for my deduction for this activity is to create an error handling process without the use of regex (to impress the professor I guess).

And in this part:

(key >= Keys.Oem1) && (key <= Keys.Oem8)

I just took a wild guess by thinking that this will stop all input of symbols, but it turns out because of this my control of the . symbol just above the alt key on your keyboard has been disabled :(

I am also having trouble with asking for the email of the user. My take on that matter looks something like this:

if (!((tBoxEmail.Text.Contains(".")) && (tBoxEmail.Text.Contains("@"))))
            {
                errorProvider1.SetError(tBoxEmail, "ERROR!");
            }

only problem with that is when the user types in just @. the program still accepts it, so the only way I could counter that was to check if the length of tboxEmail.text was greater than 2, although it presents problems in the latter part of the string. One example of those problems is if the user enters a string of chars and just inserts @. at the end it still accepts it, or maybe if he decides to type in emai@provider. the program will still accept it.

I decided to use keydown and keypress so that I may catch the incorrect input prior to letting the user submit it to the database.

And how do I remove the errorprovider once the user enters correct input? for example they get it wrong and the error provider fires then if the user finally inputs the correct input the fired error provider does not go away.

Sorry for asking too much.

Recommended Answers

All 8 Replies

@ddanbe:

Thank you for the link, but I don't plan on using regex, although I will look into your suggestion, thanks.

how can I exclude the "." sign (the one above the alt key) from my

(key >= Keys.Oem1) && (key <= Keys.Oem8)

statement? I can't use the key because of the restrictions I made and if I force it by:

(key == Keys.Oemperiod)

it still does not work.

Okay, I tried this but this just looks horrible...

private bool checkInputByKeyCode(Keys key)
        {
      
            if ((key == Keys.OemBackslash) || (key == Keys.OemCloseBrackets) || (key == Keys.OemOpenBrackets) || (key == Keys.OemMinus) || (key == Keys.OemPipe) || (key == Keys.Oemplus) || (key == Keys.OemQuestion) || (key == Keys.OemQuotes) || (key == Keys.OemSemicolon) || (key == Keys.Oemtilde) || (key == Keys.Oemcomma) || ((key >= Keys.NumPad0) && (key <= Keys.NumPad9)) || ((key >= Keys.D0) && (key <= Keys.D9)) || (key == Keys.Divide) || (key == Keys.Multiply) || (key == Keys.Subtract) || (key == Keys.Add) || (key == Keys.Space))
            {
                return false;
            }

            else
            {
                return true;
            }
                
        }

I think you ought to reconsider regex. It is a very powerful tool and its not that difficult to pick up the basics. You can use it to check for incorrect characters as well as specific patterns...which is vrey handy for validating things like postcode/email address/telephone number etc.
We'd be more than happy to help you out if you have any problems implementing it. Otherwise you end up with some very heavy duty nested if statements for things like this.

If you really dont want to use regex you can use something like:

const string AllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890."; //store all valid characters

            if (AllowedChars.Contains(e.KeyChar) || Char.IsControl(e.KeyChar)) //if key pressed is valid OR control key (allows DEL, Backspace, etc)
            {
                e.Handled = false; //allow keypress
            }
            else
            {
                e.Handled = true; //suppress keypress
            }

I think you ought to reconsider regex. It is a very powerful tool and its not that difficult to pick up the basics. You can use it to check for incorrect characters as well as specific patterns...which is vrey handy for validating things like postcode/email address/telephone number etc.
We'd be more than happy to help you out if you have any problems implementing it. Otherwise you end up with some very heavy duty nested if statements for things like this.

If you really dont want to use regex you can use something like:

const string AllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890."; //store all valid characters

            if (AllowedChars.Contains(e.KeyChar) || Char.IsControl(e.KeyChar)) //if key pressed is valid OR control key (allows DEL, Backspace, etc)
            {
                e.Handled = false; //allow keypress
            }
            else
            {
                e.Handled = true; //suppress keypress
            }

@Ryshad:

Ingenius! Just what I needed!

I really wanted to make my own methods for input checking to impress my professor. But I think I will eventually use regex for the email part of my registration form, I was reading on topics about regex and I couldn't understand it haha.

Haha, what can i say...i'm great :p
But seriously, glad it helped you out. Skimmed over the bit in your orignal post about impressing him with a non-regex solution :p
My point still stands though, learning to use regular expressions will pay off in the long run, and you have a great community here at daniweb that will help you get to grips with it.

Remember to mark the thread as solved if you feel your question has been answered

HAHA! You have helped me loads of times already Ryshad, I mean you have taught me more about c# than my professor! (seriously, he does not teach us well) I use what I know from c++ and ask here for help and people like you and ddanbe help me out, really grateful for you guys. And because of what you guys are teaching me I am enjoying c# more and more (than that traumatizing c++ haha). Thanks again!

Will mark this as solved later :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.