Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation for Input text box in a for loop 1

Status
Not open for further replies.

spirit66

Technical User
Apr 10, 2004
29
US
I have a FORM which has a text box in a for loop .

Code:
 <form name="form1" method="POST" Action="sasd.asp" onSubmit="return validate();">
			<%
			for(i=0;i <3;i++)
			{
			%>
<input width="150" Type="text" name="NM">
			<%
			}
			%>

</FORM>

I'm having little difficulity in reading what is inside the "NM" Input text box so that i can do some validation .
This is what i have in the validate() function .
Code:
var a1=document.form1.elements[1].value;
var a2=a1+'';
var a3=a2.split(",");
alert(a3[0]+a3[1]);
I'm able to read only a3[0] and not a3[1] .
But if i submit this to the next ASP Page i'm able to read everything but not in the same ASP Page in javascript function .
Please let me know where i'm doing wrong .

Thanks!!!!


 
When several elements have the same name, they form named collection:
Code:
<script language="javascript">
function validate()
{	oTxtElements = document.form1.elements.NM;
	for( s="", i=0; i< oTxtElements.length; i++ )		s+= oTxtElements[i].value+ "\n"
	alert( s );
}
</script>
Another way is to cycle through all form elements and compare names:
Code:
<script language="javascript">
function validate()
{	oForm = document.form1;
	for( s="", i=0; i< oForm.elements.length; i++ )
	{	oElement = oForm[i];
		if( oElement.name=="NM" )	s+= oElement.value + "\n";
	}
	alert( s );
}
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top