Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
txtFaxNo.Text, txtTelNo.Text, and txtMobileNo.Text are textboxes that has to be enabled to be validated. But when they are disabled, I want the system to ignore them when validating the length.



C#
if ((myControl.Text.Length != 7) && (myControl.Name.ToString() == "txtTelNo") && (myControl.Enabled)).


The code above didn't work. the system still validates the length of disabled textbox.

where in the code can I insert " mycontrol.Enabled "?
C#
public static bool IsValidLength(Form myform)
        {
            foreach (Control myControl in myform.Controls)
            {
              
                if (myControl.GetType() == typeof(TextBox))
 
                {
 
                        if (IsNumeric(myControl.Text) == true)
                        {
                            if ((myControl.Text.Length != 7) && (myControl.Name.ToString() == "txtTelNo"))
                            {
                                MessageBox.Show(myControl.Name.Substring(3).ToString() + " length is invalid.");
                                myControl.Focus();
                                return false;
                            }// end of 7
                            if ((myControl.Text.Length != 7) && (myControl.Name.ToString() == "txtFaxNo"))
                            {
                                MessageBox.Show(myControl.Name.Substring(3).ToString() + " length is invalid.");
                                myControl.Focus();
                                return false;
                            }// end of 7
                            if ((myControl.Text.Length != 11) && (myControl.Name.ToString() == "txtMobileNo"))
                            {
                                 //myControl.Text = myControl.Text.Trim();

                                MessageBox.Show(myControl.Name.Substring(3).ToString() + " length is invalid.");
                                myControl.Focus();
                                return false;
 
                            }// end of 7

                        }// end of isnumeric

                        else
                        {
                            if ((myControl.Text.Length < 2) && (myControl.Name.ToString() != "txtSuppNo"))
                            {
                                MessageBox.Show(myControl.Name.Substring(3).ToString() + " length is invalid.");
                                myControl.Focus();
                                return false;
                            }// end of 2
                        }// end of else

                }//end of gettype
            }//end of foreach
            return true;
        }// end of isvalidlength
Posted
Updated 8-Jul-12 18:47pm
v2

Hi,

Here is one of the solution,

1) You need to use ASP.NET validation control instead of manual validation from codebehind.
2) You need to assign validation group property from code behind.

Useful links

Validation Group MSDN[^]

Validator Controls in ASP.NET[^]

Validation server control in asp.net[^]

Thanks
-Amit
 
Share this answer
 
Use this. Look for change in paranthesis

if ( 
((myControl.Text.Length != 7)
 && (myControl.Enabled)) && 
(myControl.Name.ToString() == "txtTelNo") 
)
 
Share this answer
 
Just add

C#
if(!myControl.Enabled)
{
     continue;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900