validations for the controls that are dynamically created at runtime
-
i need to validate the Textboxes that are created at runtime. The text should not be blank and it should allow only numbers.
Use a MaskedTextBox[^] instead.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
i need to validate the Textboxes that are created at runtime. The text should not be blank and it should allow only numbers.
maybe validate against a regex? ^\d+$ you could put it in the TextChange event handler method eks.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex r=new Regex(@"^\d+$");
Match m=r.Match(textBox1.text);if(!m.Success) MessageBox.Show("Wrong input format"); }
or you could check the content of the textbox when it is submited, in the same fasion, using Regex and Match classes. one idea is to make a class that inherits from the TextBox class, add a string attribute that can hold the regex expression, and use that class instead of the TextBox and store the regex in that variable, then you could just loop through all the controls when the user subits the data, an check it against the regex stored in that control, like this:
public class ExtTextBox:TextBox
{
public string ValidationRegEx; //you shuld use get/set methods insted of setting the attribute as public, but I am lazy today.
}....
//create the control
ExtTextBox newETB=new ExtTextBox();
...
...
newETB.ValidationRegex=@"^\d+$";
...
this.Controls.add(newETB);//when user submits data
foreach(Control c in this.Controls)
{
if(c is ExtTextBox)
{
ExtTextBox tmpC=(ExtTextBox)c;
Regex r=new Regex(tmpC.ValidationRegex);
Match m=r.Match(tmpc.Text);if(!m.Success)
{
//give feedback to user
}
}
}modified on Wednesday, August 19, 2009 5:00 AM