Validation Code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Validation Code

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    5

    Validation Code

    HI everyoone
    I'm new in C# and i have one question. I should write a code that validates a text that should be: 7 digits in 6 rows,and should be separated by a space.Like this
    1 2 3 4 5 6 7
    0 8 9 7 1 3 9
    4 5 6 9 7 2 1
    6 9 2 0 5 6 6
    .
    .
    .
    I'm writting the code in Visual Studio 2008, windowsformapplication.
    I have no idea how to write this, so plese help Thanks

  2. #2
    Join Date
    Dec 2011
    Posts
    61

    Re: Validation Code

    If you feed the text from a file, then it's sth. like this:

    Code:
                Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$");//note there's a blank space after \d
                bool isValid = true;
                using (StreamReader reader = new StreamReader(File.Open("test.txt", FileMode.Open)))
                {
                    while (reader.Peek() > -1)
                    {
                        if (regex.IsMatch(reader.ReadLine()) == false)
                        {
                            isValid = false;
                            break;
                        }
                    }
                    if (isValid)
                    {//do sth. ...
                     }
                 }
    If you already assign the text to a string variable, i.e., sth. like this:
    string t = "1 2 3 4 5 6 7\r\n2 4 4 6 7 8 9";
    then use string[] ts = t.Split(new string[]{"\r\n"});
    to split the string into the array, then use a loop and the above regex to test if each array element matches

    This works if you allow "jawed" text, like this:
    Code:
      1 2 3 4 5 6 7
    2 5 3 6 3 6 4
         3 5 6 7 8 9 9
    If you don't allow this and want the collumns to strictly align, you need to record the number of white space preceding the first line, and check if the following lines have equal number of preceding white space.
    Last edited by Silent Sojourner; January 4th, 2012 at 04:45 PM.

  3. #3
    Join Date
    Jan 2012
    Posts
    5

    Re: Validation Code

    Hi,
    This is manually input by a user. The user should enter the values(0-9, in 6 rows, separated by space) in a textBox and after he clicks on a button it should verify the input like I mentioned above. If it's wrong than it should pop up an error message. I know that this code should be in btn_Click function, but I I don't how to verify the input. Should I use the if statement or maybe the for loop? Thanks

  4. #4
    Join Date
    Dec 2011
    Posts
    61

    Re: Validation Code

    Code:
                Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$");//note there's a blank space after the first '\d'
               bool isValid = true;
                string[] ts = textBox1.Text.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
                if (ts == null || ts.Length < 1)
                {
                    isValid = false;
                }
                else
                {
                    foreach (string t in ts)
                    {
                        if (regex.IsMatch(t) == false)
                        {
                            isValid = false;
                            break;
                        }
                    }
                }
                if (isValid)
                {
                      //do sth.
                }

  5. #5
    Join Date
    Jan 2012
    Posts
    5

    Re: Validation Code

    Thank you very much I just included the messages for error and it works

  6. #6
    Join Date
    Jan 2012
    Posts
    5

    Re: Validation Code

    HI again
    i have another problem that i can't solve. i searched the net but i couldn't find a proper solution. after i validate the values entered into the textbox, the same values should be copied into a array labels. here's the code for the array labels:
    Code:
    int a = 0;
                pole = new Label[42];
                for (int i = 1; i <= 6; i++)
                {
                    for (int j = 1; j <= 7; j++)
                    {
                        l = new Label();
                        l.Name = "label" + i.ToString() + j.ToString();
                        l.Text = "Z";
                        l.Width = 20;
                        l.Height = 20;
                        l.TextAlign = ContentAlignment.MiddleCenter;
                        l.Parent = this;
                        l.BackColor = Color.FromArgb(100, 149, 237);
                        l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
                        pole[a] = l;
                        a++;
                        
                    }
                }
    So all the labels have text "Z", and after click on a button they should be changed with the values in the textbox. I have tried to do this:
    Code:
     l.Text = txtVnes.Text.Trim();
    but only one value is copied and replaced in one array in the array label. how can i copied all values in the array label? thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center