Input validation in C#.NET

Questions about programming languages and debugging
Post Reply
User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Input validation in C#.NET

Post by z3r0aCc3Ss »

Hello, I am now working on C#.NET and VB.NET. Started with C# for now.
I want to validate textboxes for various purposes. Like only integers can be entered or only alphabets (capital/small).
How do I do that?
I wrote following code, but it gave me error:

Code: Select all

        private void button1_Click(object sender, EventArgs e)
        {
            Regex r = new Regex("0-9");

            if (r.IsMatch(textBox1.Text))
            {
                MessageBox.Show("Everything is fine", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                return;
            }
        }
And error took me here:

Code: Select all

this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
Is there any other way by which you can do validation in C#.NET?
How about validation using ASCII codes. I used to do it when I was doing simple VB 6.0.
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

I even tried with ASCII codes. Still the same error.

Code: Select all

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                MessageBox.Show("Only enter numbers");
            }
        }
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

I did it one way:

Code: Select all

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                System.Int64 i = System.Int64.Parse(textBox1.Text);

                if (i < 1 && i > 9)
                {
                    MessageBox.Show("Please only enter numbers");
                }
            }

            catch (FormatException)
            {
                MessageBox.Show("Please only enter numbers");
            }
But, I don't feel satisfied. First two methods I like the most. Please help. Specially ASCII codes then RegEx.
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Input validation in C#.NET

Post by ayu »

Did a quick one, is this what you need?

Code: Select all

private void ValidateZipButton_Click(object sender, System.EventArgs e)
{
   String NumRegex = @"^[0-9]+$";

   if(Regex.IsMatch(NumTextBox.Text, NumRegex))
   {
      ResultLabel.Text = "number is valid!";
   }
   else
   {
      ResultLabel.Text = "number is invalid!";
   }
}
"The best place to hide a tree, is in a forest"

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

Yes cats. Its working fine. Thanks, :)

But I want it to be happened on key press. Instead of showing errors through message box or something, I want to make it in such a way that user will not be able to type anything except numbers. So, we have to use ASCII.
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Input validation in C#.NET

Post by Gogeta70 »

Hmm, i'm no C#.NET coder, but i know windows API and .NET share some similarities, so maybe this will help.

If i had a single-line input control (like a username or password field) and only wanted to accept digits (0-9), i would subclass the control and intercept the WM_CHAR message. Check the wParam value (that's the ascii value of the key that was just pressed) and if it's not numeric, return false.


Basically what i'm saying is that you need to "hijack" the control's character input handler and handle the input yourself.
¯\_(ツ)_/¯ It works on my machine...

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: Input validation in C#.NET

Post by Lundis »

Here's one way to do it:

1) use a hooked function such as your textBox1_TextChanged
2) Get the string in the TextBox: String text = textBox1.Text;
3) Remove all invalid characters in the string: String cleansedText = MethodToRemoveInvalidCharacters(text);
4) Set the TextBox string to the cleansed string: textBox1.Text = cleansedText;

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Re: Input validation in C#.NET

Post by bubzuru »

just check if the char going in on each keypress is a number
if not dont allow it

Code: Select all

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Char.IsNumber(e.KeyChar)) e.Handled = true;
}
](*,)
[img]http://www.slackware.com/~msimons/slackware/grfx/shared/greymtlSW.jpg[/img]

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

bubzuru, its not working dat way...
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Re: Input validation in C#.NET

Post by bubzuru »

it works fine for me, what errors do you get ?

have you added an on keypress event to your textbox ?
[img]http://www.slackware.com/~msimons/slackware/grfx/shared/greymtlSW.jpg[/img]

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

I don't get any errors. It doesn't work. Nothing happens... :(
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

User avatar
z3r0aCc3Ss
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 700
Joined: 23 Jun 2009, 16:00
14
Contact:

Re: Input validation in C#.NET

Post by z3r0aCc3Ss »

Finally, it worked.
Thank to everyone who helped. :) :D
Beta tester for major RATs, all kinds of stealers and keyloggers.
Learning NMAP

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Re: Input validation in C#.NET

Post by bubzuru »

for anyone reading this looking for the answer. why didn't it work ? what did you change ?
im guessing you just forgot to add the on keypress event handler
[img]http://www.slackware.com/~msimons/slackware/grfx/shared/greymtlSW.jpg[/img]

Post Reply