Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I Have dynamic generated textboxes. I wrote code to check if the textboxes empty then should show a message. but it is validating only first dynamic textbox and it is also taking space. how to do it . kindly help.
if (document.getElementById("txt").value == "") {

alert("Please enter Serial Number");

document.getElementById("txt").focus();
return false;
Posted
Comments
jacobjohn196 18-Jul-14 8:31am
   
var text=document.getElementById("txt").value;
if(text.ToString().trim()=="")
{
alert("Please enter Serial Number");
document.getElementById("txt").focus();
return false;
}
Member 10578683 18-Jul-14 9:05am
   
in this code the alert message is not appearing
Member 10578683 18-Jul-14 9:11am
   
now it is working fine. if (document.getElementById("txt").value.toString().trim() == "") {

alert("Please enter Serial Number");

document.getElementById("txt").focus();
return false;
I wrote the code this way. but the problem is this is working fine for one textbox which is generating first in out of the dynamic textboxes
Member 10578683 18-Jul-14 9:12am
   
if (txtQuantity.Text != "")
{


for (int i = 0; i <= Convert.ToInt16(txtQuantity.Text) / 3; i++)
{


Label la = new Label();
la.Text = ddlProductName.SelectedItem.Value + (i + 1).ToString();
la.ID = "abc1" + i;

PlaceHolder1.Controls.Add(la);

//la = FindControl(String.Format(i)) as Label;




TextBox txtbox = new TextBox();
// txtbox.Text = "Textbox - " + i.ToString();


PlaceHolder1.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text' />"));

PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));

}
for (int j = (Convert.ToInt16(txtQuantity.Text) / 3) + 1; j <= Convert.ToInt16(txtQuantity.Text) * 2 / 3; j++)
{


Label la1 = new Label();
la1.Text = ddlProductName.SelectedItem.Value + (j + 1).ToString();
la1.ID = "abc2" + j;

PlaceHolder2.Controls.Add(la1);

//la = FindControl(String.Format(i)) as Label;




TextBox txtbox1 = new TextBox();
// txtbox.Text = "Textbox - " + i.ToString();
PlaceHolder2.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + j + "'type='text' />"));
PlaceHolder2.Controls.Add(new LiteralControl("<br/>"));





}
for (int k = (Convert.ToInt16(txtQuantity.Text) * 2 / 3) + 1; k < Convert.ToInt16(txtQuantity.Text); k++)
{


Label la2 = new Label();
la2.Text = ddlProductName.SelectedItem.Value + (k + 1).ToString();
la2.ID = "abc3" + k;

PlaceHolder3.Controls.Add(la2);

//la = FindControl(String.Format(i)) as Label;




TextBox txtbox1 = new TextBox();
// txtbox.Text = "Textbox - " + i.ToString();


PlaceHolder3.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + k + "'type='text' />"));

PlaceHolder3.Controls.Add(new LiteralControl("<br/>"));

}
}

1 solution

if anyone can define more than one element with the same ID , the getElementById() method returns the first element. for multiple ID calling you can use getElementsByName.. and use loop.

PlaceHolder1.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text' />"));

u need to change your code like this for all your dynamic textbox

PlaceHolder1.Controls.Add(new LiteralControl("<input name='txt' id='Textbox" + i + "'type='text' />"));


and then call your getElementByName and using loop like this



var elemArray = document.getElementsByName(name);
for(var i = 0; i < elemArray.length; i++){


- write down your code for checking serial number. with
document.getElementById(elemArray[i].id)

}


I m not giving you whole code bt give you hint. so u can try with this and may be it will helpful to you.
   
Comments
Member 10578683 21-Jul-14 9:24am
   
Hello thank you for your reply. I wrote the code like this var elemArray = document.getElementsByName("TextBox");
for (var i = 0; i < elemArray.length; i++) {
if (document.getElementById(elemArray[i].id) == "") {

alert("Please enter Serial Number");

document.getElementById("txt").focus();
return false;

}
}
it is taking empty space. how to avoid space in the checkboes
Member 10578683 21-Jul-14 9:57am
   
It is not working correctly
Is there any server side code for this
purvivani 22-Jul-14 0:59am
   
u can use .trim for avoid space when checking with any string.
if (document.getElementById(elemArray[i].id).tostring.trim == "")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900