473,473 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4881
"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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.