Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

user inputs validations not functioning

$
0
0

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...


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>