hi

i am trying to validate a id card textbox with the format 123456m

i want to check and if it is not in that format show a messagebox invalid input.

how can i read char [7] and compare it to a deafult char m

your input is appreciated.

txtBox.Text[6] == 'm';

That should work for you. If you are checking for numbers as well, you can iterate through the characters in the string (returned by the TextBox.Text property) and use char.IsNumber().

hi thanks for your reply

i was reasoning on these lines

char G = "g";
char M = "m";



   if  textbox.text[6] == m || g {

        messagebox.show( " iD is ok ");

}

else 
      messagebox.show( " Please input a valid iD ");

Edited 5 Years Ago by __avd: Added [code] tags.

HMMM..well what you are doing there is trying to put a string value in to a char type...

use single quotation marks...like this...

char G = 'g';
char M = 'm';

if ((textbox.text[6] == m) || (textbox.text[6] == g))
 {

messagebox.show( " iD is ok ");

}

else 
messagebox.show( " Please input a valid iD ");

remember if an id does not have the m or g in the 7th (or 6th) position in your char array, your going to run in to problems. What you could do is use string values....check what position the m or g is at then decide whether to let it go or throw exception etc...

hope this helps

You should definitely add a check to ensure there is 7 characters in the string, otherwise you will get an out of bounds exception:

...
if (textBox.Text.Length >= 7 && (textBox.Text[6] == M || textBox.Text[6] == G))
...

Using a regular expression is easier:

if (Regex.Match(textBox.Text, @"^\d{6}[mg]$").Length > 0) {
    // it's a good ID number
}

This expression requires that there be 6 digits followed by either an 'm' or a 'g'. That is also the only thing allowd ("123456mt" doesn't work, where it would work with the other methods given here).

Comments
Perfect job for regular expressions =D

hi again

i am getting compilation errors
this is the complete method

it is saying that the name 'm' does not exist in the current context

{
             string _numberIn = numberIn;
            int count = 0;
             Boolean isValid = false;

             do
             {
                /*if (!char.IsNumber(_numberIn[count]))
                 {
                     MessageBox.Show("Please input a valid number");
                 }
                 count++;

                 if (count == 7) {
                  */
                   if (txtboxIdcard.Text.Length >= 7 && (txtboxIdcard.Text[6] == M || txtboxIdcard.Text[6] == G))
                                                               
                     {
                      MessageBox.Show("The id card is valid");
                 }

             }
             while ((count < _numberIn.Length) && (isValid == false));

         }

Edited 5 Years Ago by __avd: Added [code] tags.

thanks for you comment , it is working now

now i have to sort when user inputs just text or just numbers/

Hi i should use regulair expressions as stated earlier. This will make you life in these kinds of problems much easier. I undstand that the learning curve is a little bit steeper, but the investment is worthwhile. I can tell you that from experience.
Bing around, the G word is a little bit difficult on this website ;) for regulair expressions and there are tons of very helpfull sites.

This article has been dead for over six months. Start a new discussion instead.