472,954 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

validation for dynamic asp.net textbox

Hello,

I am using asp.net 2003 using C#. I am creating textboxes dynamically and
want to validate to make sure the user can only input an integer. I don't
think I can use the validators because I don't know what the ID of the
textbox is ahead of time.

I am trying to do it with javascript with a tableCell.Attributes.Add. I'm
not sure I can do what I am trying.

Here is a pseudo code example of what I am attempting:

TableCell td = new TableCell();
TextBox txtSO = new TextBox();
td.Attributes.Add("onclick","ValidateIntOnly(" + txtSO.text + " );");
td.Controls.Add(txtSO);
tr.Cells.Add(td);
In the html section of the aspx page I have:

<script type ="text/javascript" language="javascript" >
function ValidateIntOnly(i)
{
if(i.value.length>0)
{
i.value = i.value.replace(/[^\d]+/g, '');
}
}
</script>
Can someone help me out with this or tell me a better way to do it?


--
Jerry J
Jul 8 '08 #1
2 4843
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
I am using asp.net 2003 using C#. I am creating textboxes dynamically and
want to validate to make sure the user can only input an integer. I don't
think I can use the validators because I don't know what the ID of the
textbox is ahead of time.

I am trying to do it with javascript with a tableCell.Attributes.Add. I'm
not sure I can do what I am trying.

Here is a pseudo code example of what I am attempting:

TableCell td = new TableCell();
TextBox txtSO = new TextBox();
td.Attributes.Add("onclick","ValidateIntOnly(" + txtSO.text + " );");
td.Controls.Add(txtSO);
tr.Cells.Add(td);
In the html section of the aspx page I have:

<script type ="text/javascript" language="javascript" >
function ValidateIntOnly(i)
{
if(i.value.length>0)
{
i.value = i.value.replace(/[^\d]+/g, '');
}
}
</script>
Can someone help me out with this or tell me a better way to do it?
A couple of things:
- You are using the "onclick" event of the textbox, which will only fire
if the user clicks on it. You should probably be looking at the lost focus
event (onblur) if you want to validate the textbox after it is abandoned, or
maybe onkeyup if you want to validate each key press on the fly.
- You are adding client-side code, so you have to pass the client-side
value of the textbox to your javascript procedure. Since the textbox gets
converted to an <input type="text".../>, you have to use the preperty
"value" instead of the property "Text", which is only valid on the server
side.
- Even if you change "ValidateIntOnly(" + txtSO.text + " );" into
"ValidateOnly("+txtSO.ClientID+".value");" it will still not work, because
the value is a string that would be passed by value into the validation
procedure, so you won't fix anything by modifying its value inside the
procedure.You will have to pass the ID of the textbox into the procedure and
have the procedure do a GetElementByID and then access and modify the value.
Of course, this requires that you assign an ID to the Textbox you are
creating.

- If you prefer, you CAN add validators dynamically. When you Add the
TextBox, you can assign a value to its ID property, and then create and Add
to the same cell a validator connected to that ID.

Jul 8 '08 #2
Alberto, thank you for that information.

--
Jerry J
"Alberto Poblacion" wrote:
"Jerry J" <Je****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...
I am using asp.net 2003 using C#. I am creating textboxes dynamically and
want to validate to make sure the user can only input an integer. I don't
think I can use the validators because I don't know what the ID of the
textbox is ahead of time.

I am trying to do it with javascript with a tableCell.Attributes.Add. I'm
not sure I can do what I am trying.

Here is a pseudo code example of what I am attempting:

TableCell td = new TableCell();
TextBox txtSO = new TextBox();
td.Attributes.Add("onclick","ValidateIntOnly(" + txtSO.text + " );");
td.Controls.Add(txtSO);
tr.Cells.Add(td);
In the html section of the aspx page I have:

<script type ="text/javascript" language="javascript" >
function ValidateIntOnly(i)
{
if(i.value.length>0)
{
i.value = i.value.replace(/[^\d]+/g, '');
}
}
</script>
Can someone help me out with this or tell me a better way to do it?

A couple of things:
- You are using the "onclick" event of the textbox, which will only fire
if the user clicks on it. You should probably be looking at the lost focus
event (onblur) if you want to validate the textbox after it is abandoned, or
maybe onkeyup if you want to validate each key press on the fly.
- You are adding client-side code, so you have to pass the client-side
value of the textbox to your javascript procedure. Since the textbox gets
converted to an <input type="text".../>, you have to use the preperty
"value" instead of the property "Text", which is only valid on the server
side.
- Even if you change "ValidateIntOnly(" + txtSO.text + " );" into
"ValidateOnly("+txtSO.ClientID+".value");" it will still not work, because
the value is a string that would be passed by value into the validation
procedure, so you won't fix anything by modifying its value inside the
procedure.You will have to pass the ID of the textbox into the procedure and
have the procedure do a GetElementByID and then access and modify the value.
Of course, this requires that you assign an ID to the Textbox you are
creating.

- If you prefer, you CAN add validators dynamically. When you Add the
TextBox, you can assign a value to its ID property, and then create and Add
to the same cell a validator connected to that ID.

Jul 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tom | last post by:
I know how to use these great controls. They work very well. But is there a method or property I can use to set the focus to the field that is in error. For example... If I have a page with 5...
3
by: Rick | last post by:
I have an interesting problem when I run the following code in Netscape (7.02) vs. IE. This page works great in IE and all my controls bring up the validation summary dialog box if the required...
3
by: simon | last post by:
I have 2 <asp:textbox controls>: <asp:textbox id="txt1" Runat="server" ></asp:textbox> <asp:textbox id="txt2" Runat="server" ></asp:textbox> User must insert the value either in txt1 OR in...
1
by: Arpan | last post by:
I have a Web Form in my ASPX page which makes use of Web controls & incorporates validation controls. This is the code snippet that does the needful: <form runat=server> NAME:<asp:TextBox...
1
by: Liz | last post by:
I have a page with several dropdownlists, several text boxes and several buttons which perform calculations. I need to validate one dropdownlist (not the whole page) with the click of one button. I...
1
by: Kum | last post by:
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...
2
by: winnie_us99 | last post by:
Hi All, I am trying to do validation on my text field before going to the next page to create a user. It doesn't look like the next button will fire any validation. Am I missing something? Can...
12
by: Dabbler | last post by:
I need to insure that at least one of three phone number fields has a value (requiredfield) but I'm not sure of a way to implement this without server side logic. Is there a way to use the...
0
by: Steve Funk | last post by:
All, I have searched all around and have not yet found the answer to this nor a solution. Hopfully it will be easy to overcome. Here is what I am trying to do: I'm trying to build a wizard...
1
by: raam | last post by:
Hi, I have ajax tab container with 6 tabs in it. I need to validate each tab contents separately and the validtaion summary must be separate for each tab. i cannot do that only one summary is...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.