validation for dynamic asp.net textbox

J

Jerry J

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?
 
A

Alberto Poblacion

Jerry J said:
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.
 
J

Jerry J

Alberto, thank you for that information.

--
Jerry J


Alberto Poblacion said:
Jerry J said:
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.
 

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

Top