Hello daniweb chums

I once again cannot seems to get a simple bit of text box validation working

I am trying to detect if a first name entered contains a number by converting the text box text into a char array and then convert each character entered (using a for loop) to char and catch the format exception when a number is entered. *Deep Breath*

Any thoughts?

And please don't just tell me not to do it this way unless it is impossible

Thanks in advance :)

Char[] fnchar;
                char n;
                fnchar = tbname.Text.ToCharArray();
                for (int p = 0; p < fnchar.Length; p++)
                {
                    try
                    {
                        n = fnchar[p];
                        tbname.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(179)))), ((int)(((byte)(113)))));
                        fni = true;
                    }
                    catch (FormatException)
                    {
                        fni = false;
                        tbname.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(205)))), ((int)(((byte)(112)))), ((int)(((byte)(84)))));
                    }
                }

Hi, GonzR.
Here's a few points that may be helpful:
1. Char.IsLetter Method (Char).
2. Char.IsDigit Method (Char)
3. In C#, strings are char arrays, so there's no need to convert them. You can just iterate through string using any loop.
4. Also, there's an option to check your string, using regular expressions. Maybe it's too early, but still, here's a link: C# newbie: verifying that a string contains only letters.

Also, I would like to ask .. what magic are you doing with converting your numbers? :D Considering that the number literals in C# are treated as Int32 (or int) ...

do you mean for the "ForeColor"? That is just how it was written in the auto generated code

Thanks for your help Antenka!
this way of doing it looks a lot simpler.

my code now looks like this:

fni = true;
                char n;

                for (int p = 0; p < (tbname.Text.Length + 1); p++)
                {
                    n = tbname.Text[p];
                    c = (Char.IsLetter(n));
                    if (c == true)
                    {
                    }
                    else
                    {
                        fni = false;
                    }
                }

                if (fni == true)
                {
                    tbname.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(179)))), ((int)(((byte)(113)))));
                }
                else
                {
                    tbname.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(142)))), ((int)(((byte)(35)))), ((int)(((byte)(35)))));
                }

It doesnt pick up when a number is entered and it doesnt do anything when a number isnt entered. I dont get it :\ this code just doesnt seem to work at all

do you mean for the "ForeColor"?

Exactly. Know why? Because:

((int)(((byte)(60)))

it's the same as

60

Also, tbname.Text.Length + 1 . Why plus 1? Count how many times it would iterate if your string would be, e.g. 2 symbols length.

It doesnt pick up when a number is entered and it doesnt do anything when a number isnt entered. I dont get it :\ this code just doesnt seem to work at all

Answer the previous question, cause it may throw an exceptions to you (I would be surprised, if it's not :)). That may be cause a problem.

P.S. Char.IsLetter(n) works pretty good in here, and determines a strings also as the others symbols.

Okay iv now changed my code to :

fni = true;
                char n;

                for (int p = 0; p <= tbname.Text.Length; p++)
                {
                    n = tbname.Text[p];
                    //c = (Char.IsLetter(n));
                    if (Char.IsLetter(n))
                    {

                    }
                    else
                    {
                        fni = false;
                    }
                }

                if (fni == true)
                {
                    tbname.ForeColor = System.Drawing.Color.FromArgb((60), (179), (113));
                }
                else
                {
                    tbname.ForeColor = System.Drawing.Color.FromArgb((142), (35), (35));
                }

but still not working :(

A magician never tells ;)
tbh i didnt really understand what you meant hah

Ok .. suppose, we have entered a text, that consists of 2 letters.
What would we have here:

for (int p = 0; p <= tbname.Text.Length; p++)

Here, we will iterate from 0 to 2, ie:
we will take tbname.Text[0], tbname.Text[1], tbname.Text[2] .. doesn't look weird for you? :)
Try it yourself: enter 2 letters and debug this thing. And post here your original string and the thing that you have under tbname.Text[2].

A magician never tells

I just want you to find your solution by yourself .. I'm just trying to lead you in a right direction ;)

but still not working

Try the thing, that I said upper in this post. Fix a bug, if there is any .. and .. I'm waiting for your results ;)

I found out what each number was outputting and it was exactly how i expected.
now my code is this:

lni = true;
                char n;
                for (int p = 0; p <= tblname.Text.Length; p++)
                {
                    n = tblname.Text[p];
                    if (Char.IsNumber(n))
                    {
                        lni = false;
                        p = (tblname.Text.Length + 1);
                    }
                    else
                    {
                        lni = true;
                        //label1.Text = tblname.Text[p].ToString();
                        //label1.Visible = true;
                    }
                }
                
                
                if (lni == true)
                {
                    tblname.ForeColor = System.Drawing.Color.FromArgb((60), (179), (113));
                }
                else
                {
                    tblname.ForeColor = System.Drawing.Color.FromArgb((142), (35), (35));
                }

Its now picking up when a number is entered and iv also got it to stop the loop when a number is entered. However, It doesnt do anything when lni = true ????
argh im so confusedd

That's really weird when your prog founds a 3d char in a 2-char string ;)

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.