Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can any body tell me how I can validate a textbox control dynamicaally. There should only be 10 digits in the textbox. For this I have coded it as:
RegularExpressionValidator expvalidator = new RegularExpressionValidator();
            TextBox txt = new TextBox();
            txt.ID="txtComplainantContact";
            txt.Text=txtComplainantContact.Text;
            expvalidator.ControlToValidate =txt.ID; 
            
            expvalidator.ValidationExpression = @"\d\d\d\d\d\d\d\d\d\d";
            expvalidator.Display = ValidatorDisplay.Dynamic;
          
            if (expvalidator.IsValid)
            {
                ComplainantContact = txtComplainantContact.Text;
            }
            else
            {
                expvalidator.ErrorMessage = "plz enter 10 digits!!";
            }


Any help is greatly appreciated.
Posted
Updated 6-Jun-11 0:25am
v3
Comments
Slacker007 6-Jun-11 6:24am    
Text speak is not professional and using it here in Code Project is not a very good idea.
Ali Al Omairi(Abu AlHassan) 6-Jun-11 6:25am    
call the method validate.

finally got the solution like this:

XML
if (System.Text.RegularExpressions.Regex.IsMatch(txtComplainantContact.Text, @"\d\d\d\d\d\d\d\d\d\d"))
            {
            }
            else
            {
                Page.RegisterStartupScript("javascript", "<script>alert('Please Enter10 digit mobile number!!')</script>");
            }
 
Share this answer
 
Hi,
try this
Txt.Maxlength =10;
Regards,
Lakshmi Narayanan.S
 
Share this answer
 
1. You can use \d{10} instead of @"\d\d\d\d\d\d\d\d\d\d"
2. What is this : expvalidator.IsValid.IsValid?

Edit ----
Because the default value of this property is true, it will return true if you query this property before validation is performed. For example, this might occur if you attempt to use this property in the Control.Load event of a page.
Ref:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.isvalid.aspx[^]

Here you have created a new control txt, that could be the problem. Try any existing textbox with same code.
 
Share this answer
 
v3
Comments
mylogics 6-Jun-11 6:24am    
sory by mistake it expvalidator.IsValid but it always return true where am i wrong.
Prerak Patel 6-Jun-11 6:28am    
Try this.Validate() before checking expvalidator.IsValid
mylogics 6-Jun-11 6:36am    
no there is no such event for the txt
Prerak Patel 6-Jun-11 6:40am    
It must be on page, which validates all controls. Try this.Validate()
mylogics 6-Jun-11 6:48am    
it is working for this.validation but does not make sense and when am using it like expvalidator.Validate() it gives error object reference not set to an instance.

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