IMHO, classes are more appropriate to mark elements for CSS formatting purposes, rather than to give functional targets to elements in the page for scripts.
I suggest to assign an ID to the form, so you can reference it with javascript picking it with a getElementById function, then navigate through its elements and execute for each of them your validating function, say validateField(). Something roughly like this
function validateField(fieldToValidate) {
//Here you can perform your validation against fieldToValidate.value (for text inputs)
}
var frm = document.getElementById("myForm");
for (var i=0; frm.elements[i]; i++)
{//Cycle through all input tags, of text type, inside the form
if (
frm.elements[i].tagName=="INPUT"
&&
frm.elements[i].getAttribute("type")=="text")
)
{
validateField(frm.elements[i]);
}
}
Moreover, if you know that the dynamically-generated elements' names follow a given naming convention, like in your example (countX with X being a number) and you want to validate only the dynamically-generated input fields, kwowing they are text input fields, you can check only thoose; better would be to assign specific ID instead of names to the fields, to be sure you are not using the same name and validating something you shouldn't.
PHP side
<form method="post" action="##" id="myForm" name="test" onsubmit="return validate()">
<?php
$i=0;
while($i<5)
{
echo "<input type='text' id='txtField$i'>"
$i++;
}
?>
JS side
function validateField(fieldToValidate) {
//Here you can perform your validation against fieldToValidate.value (for text inputs)
}
var frm = document.getElementById("myForm");
var currentField;
for (var i=0; currentField = document.getElementById("txtField"+i); i++)
{//Cycle through all txtField* starting IDs elements
validateField(currentField);
}