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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating multiple textboxes

Status
Not open for further replies.

prakus

Programmer
Aug 4, 2003
19
US
I have a form with multiple textboxes. The naming convention is dynamic (examples Tbx102, Tbx105 etc...). The number of textboxes are dynamic too. I need to limit the entry in each textbox to 50 characters and notify the user if he/she has exceeded the limit.

Can someone help me with this?

Thank you.
PKS.
 
Why not use maxlength="50"?

Jon

"I don't regret this, but I both rue and lament it.
 
This should get you started.

<html>
<head><title>Test</title>
<script>
function validateFields()
{
/* Loop trhought all the elemnst in the form */

for (var i = 0; i < document.test.elements.length; i++)
{
/* check if the element is a text box */
if(document.test.elements.type == "text")
{
/* get the field id */
var fldId = document.test.elements.id;

/* get the field value */
var fldValue = eval("document.test." + fldId + ".value");

/* check if the value is greater than 5 and if it is
* go back to tah field
*/
if(fldValue .length > 5)
{
alert("The input value can't be greater than 5 characters!!!");
eval("document.test." + fldId + ".select()");
eval("document.test." + fldId + ".focus()");
return false;
}
}
}
return true;
}
</script>
</head>
<body>
<form name="test" action="">

<input type="text" name="text1" id="text1" value=""><br>
<input type="text" name="text2" id="text2" value=""><br>
<input type="text" name="text3" id="text3" value=""><br>
<input type="text" name="text4" id="text4" value=""><br>
<input type="text" name="text5" id="text5" value=""><br>
<input type="button" name="btn" id="btn" value="Button" onClick="return validateFields();"><br>
</form>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top