471,354 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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 4724
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Tom | last post: by
3 posts views Thread by simon | last post: by
1 post views Thread by Arpan | last post: by
12 posts views Thread by Dabbler | last post: by
reply views Thread by Steve Funk | last post: by
1 post views Thread by raam | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.