none
user inputs validations not functioning RRS feed

  • Question

  • Hello Guys!

    I am validating some textboxes but the validations are not working at all: 

    See Below the code

                                       
                      
     //validating office Id control
    
            private void textBox2_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                int number = -1;
                if (int.TryParse(this.textBox2.Text, out number))
                {
                    if (Validation.isOfficeId(number))
                    {
                        //This control passes validation.
                        cancel = false;
                    }
                    else
                    {
                        //This control has failed validation: number is not in the correct format
                        cancel = true;
                        this.errorProvider1.SetError(this.textBox2, "You must provide a number wit the format 11ddd!");
                    }
                }
                else
                {
                    //This control has failed validation: text box is not a number
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox2, "You must provide a valid number!");
                }
                e.Cancel = cancel;
            }
            private void textBox2_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox2, string.Empty);
            } 
    
            //validating office name
            private void textBox3_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                if (string.IsNullOrEmpty(this.textBox3.Text))
                {
                    //This control fails validation: Name cannot be empty.
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox3, "You must provide your name!");
                }
                //else if (!Validation.isOfficeName(textBox3.Text))
                //{ 
                //    //this control fails validation: name is not in the right ofrma
    
                //}
                e.Cancel = cancel;
    
            }
            private void textBox3_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox3, string.Empty);
            } 
    
            //validating office location
            private void textBox4_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                if (!(Validation.isOfficeLocation(textBox4.Text)))
                {
                    //This control fails validation: Name cannot be empty.
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox4, "office location must contain atleast one character");
                }
    
                e.Cancel = cancel;
    
            }
            private void textBox4_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox4, string.Empty);
            } 
    
               //validating numberOfStaff textBox
            private void textBox5_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                if (!(Validation.isOfficeNumberOfStaff(textBox5.Text)))
                {
                    //This control fails validation: number of staff should be just one digit.
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox5, "number of staff should be just one digit!");
                }
    
                e.Cancel = cancel;
    
            }
            private void textBox5_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox5, string.Empty);
            } 
    
            //validating officeUnit textBox
            private void textBox6_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                if (!(Validation.isOfficeUnit(textBox6.Text)))
                {
                    //This control fails validation: Name cannot be empty.
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox6, "office unit must contain atleast one character");
                }
    
                e.Cancel = cancel;
    
            }
            private void textBox6_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox6, string.Empty);
            } 
    
            // validating PayrollNumber textbox
    
            private void textBox7_Validating(object sender, CancelEventArgs e)
            {
                bool cancel = false;
                if (!(Validation.isPayrollNumber(textBox7.Text)))
                {
                    //This control fails validation: payroll Number must contain atleast one character.
                    cancel = true;
                    this.errorProvider1.SetError(this.textBox7, "payroll Number must contain atleast one character!");
                }
    
                e.Cancel = cancel;
    
            }
            private void textBox7_Validated(object sender, EventArgs e)
            {
                //Control has validated, clear any error message.
                this.errorProvider1.SetError(this.textBox7, string.Empty);
            } 
    
    
    
            private void button2_Click(object sender, EventArgs e)
            {
    
                //if (Validation.isOfficeId(textBox2.Text) && Validation.isOfficeLocation(textBox4.Text) && Validation.isOfficeName(textBox3.Text)
                //    && Validation.isOfficeNumberOfStaff(textBox5.Text) && Validation.isOfficeUnit(textBox6.Text) && Validation.isPayrollNumber(textBox7.Text))
                //{
                if (this.ValidateChildren(ValidationConstraints.Enabled))
                {
                    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mOICISTAFF12;Integrated Security=True");
                    SqlCommand com;
                    con.Open();
                    com = new SqlCommand("INSERT INTO offices VALUES('" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() + "','" + textBox7.Text.Trim() + "')", con);
                    com.ExecuteNonQuery();
    
                    textBox2.Text = string.Empty;
                    textBox3.Text = string.Empty;
                    textBox4.Text = string.Empty;
                    textBox5.Text = string.Empty;
                    textBox6.Text = string.Empty;
                    textBox7.Text = string.Empty;
                }
                else
                {
                    MessageBox.Show("there are invalid controls on the form");
                    Form1_Load( sender,  e);
                }

    The validation class is here:

    class Validation
        {
          public static string [] errorMessages = new string[5];
          static int i=0;
    
            public static Boolean isAlphaNumeric(string input)
            {
                Regex reg = new Regex(@"^[a-zA-Z0-9 ]+$");
                return reg.IsMatch(input);
    
            }
    
            public static Boolean isFirstName(string input)
            {
                Regex reg = new Regex(@"^[a-zA-Z][a-zA-Z][a-zA-Z]+$");
                return reg.IsMatch(input);
            }
    
            public static Boolean isOfficeId(int input)
            {
                Regex reg = new Regex(@"^11[0-9][0-9][0-9]$");
                
                //if (!reg.IsMatch(input.ToString()))
                //{
                //    errorMessages[i] = "the office Id you entered is not in the right format    ";
                //    i++;
                //}
    
                return reg.IsMatch(input.ToString());
            
            }
    
            public static Boolean isOfficeName(string input)
            {
                Regex reg = new Regex(@"^[a-zA-Z][a-zA-Z][a-zA-Z0-9 ]*$");
                //if (!reg.IsMatch(input))
                //{
                //    errorMessages[i] = "the office name is not in the right format   ";
                //    i++;
                //}
                return reg.IsMatch(input);
    
            }
    
            public static Boolean isOfficeLocation(string input)
            {
                Regex reg = new Regex(@"^[a-zA-Z0-9][a-zA-Z0-9 ]*$");
                //if (!reg.IsMatch(input))
                //{
                //    errorMessages[i] = "the office location you enetred is not in the correct format   ";
                //    i++;
                //}
                return reg.IsMatch(input);
    
            }
    
            public static Boolean isOfficeUnit(string input)
            {
                Regex reg = new Regex(@"^[a-zA-Z0-9][a-zA-Z0-9 ]*$");
                //if (!reg.IsMatch(input))
                //{
                //    errorMessages[i] = "the office Unit you entered is not in the correct format   ";
                //    i++;
                //}
                return reg.IsMatch(input);
    
            }
    
            public static Boolean isOfficeNumberOfStaff(string input)
            {
                //string mess;
                Regex reg = new Regex(@"^[0-9]$");
                //if (!reg.IsMatch(input))
                //{
                //    errorMessages[i] = "the office number is not in the right format   ";
                //    i++;
                //}
                return reg.IsMatch(input);
    
            }
    
            public static Boolean isPayrollNumber(string input)
            {
                //string mess;
                Regex reg = new Regex(@"^[0-9]+$");
                //if (!reg.IsMatch(input))
                //{
                //    errorMessages[i] = "the payroll number you entered is not in the right format   ";
                //    i++;
                //}
                return reg.IsMatch(input);
    
            }
        }

    Kindly help. I am a novice in programming...

    Monday, April 8, 2013 11:33 AM

All replies

  • Hi modoufuloo,

    Could you please give more detail information?

    What's the current result?

    What's the expect result?

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 9, 2013 6:54 AM
    Moderator
  • have you directly call static method of class into outside of class?

    have you created object of class Validation?

    have you directly call method of class using Validation.isOfficeId(number))?

    Why you need static method?...


    Alpa

    Tuesday, April 9, 2013 8:15 AM
  • Maybe creating a short but expressive piece of code can help to understand your problem.

    Be aware that contains for instance no Regex and or Database handling. 

    Regex is not a part of this Windows Forms forum.

    Windows Forums is one of the Desktop User Interfaces from Microsoft.


    Success
    Cor

    Tuesday, April 9, 2013 8:50 AM
  • yes Bob

    No entry is validated at all. All entries go straight to the database.

    the expected result: when I input wrong data format it should retuurn me an error but when a correct data format is inserted the query should be successfully executed.


    • Edited by modoufuloo Tuesday, April 9, 2013 4:22 PM
    Tuesday, April 9, 2013 4:00 PM
  • Yes Alpha!

    1.I didnot call static methods out side of class..if i have done so i would have prompted by the editor

    2. the validation class still static so no need to create an object

    3.yes i did call methods of validation class directly

    4. Because the class is static

    Tuesday, April 9, 2013 4:07 PM
  • have you used break point at method you have called?did it actually go to definition of mathod?



    Alpa

    Tuesday, April 9, 2013 6:26 PM
  • yes Bob

    No entry is validated at all. All entries go straight to the database.

    the expected result: when I input wrong data format it should retuurn me an error but when a correct data format is inserted the query should be successfully executed.


    Hi modoufuloo,

    I still cannot give a solution without more information. However, here are some suggestions to handle this issue.

    First, please make sure all these events raise correctly. add break point at the beginning of each method and see if these methods raise.

    Second, debug the method and check if the validating logic works in a correct way.

    Best Regards,


    Bob Wu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 11, 2013 7:02 AM
    Moderator