Experts Can you help me solve this dynamic textbox validation prob

G

Guest

Hi,
I need help in asp.net dynamic textbox controls
validation.

I am creating textbox controls dynamically on a
asp.net webpage. Now after creating the textboxes on
the page I want to validate these text boxes when the
user submits or posts back to the server. My intention
was to create a textboxvalidator function which takes
the control name as argument and returns an true if
valid else the corresponding error message. The
situation is complex as the multiple textboxes
dynamically generated will have different data types.

Scenario:

1. User opens a webpage and selects a number in a
dropdownbox and submits
2. Depending on the number selected, correspondingly
dynamic textboxes are generated with id textbox1,
textbox2, etc and shows up on the webpage.
3. User then fills the textbox1,
textbox2,etc...textbox30...
4. Now I need to check the values entered in textbox1,
textbox2, etc..
5. The textbox1 should be valid date, textbox2 should
be valid number, textbox3 should be valid email and so
on..

My solution could have been that I generate
customvalidation controls for each and every textbox
dynamically created and assign the control to be
validated as the corresponding textbox id. However, I
am not sure if that is a good approach. Because if the
user selects 50 we need to created 50 customvalidation
controls.

Rather i feel it would be better to write a common
validateDatetextbox(textboxcontrolid),
validateNumbertextbox(textboxcontrolid), etc functions
and pass the textbox id to this function to check the
validity of that textbox.

I am not sure which is the best approach and how I
proceed. I greatly appreciate if some one could send
me a sample code of how to do this.

Thanks in advance

Mike
 
P

Peter Blum

If I understand you correctly, you need a validator that confirms a real
date, integer, and email address have been entered. The ASP.NET validators
handle all of these cases without creating a custom control.
1. Use the CompareValidator to check the format of a date, integer, currency
and decimal. Set the Operator property to DataTypeCheck and Type property as
needed.
2. Use the RegularExpressionValidator for emails and other strongly
patterned strings. In fact, the ValidationExpression property has a cool
editor in the Properties Editor that lists common expressions including the
email address expression.

Now if you are trying to intelligently enable the validators based on the
DropDownList, you can simply change the Enabled property on the server-side.
It sounds like they are disabled initially (Enabled=false).

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top