ciao a tutti,
mi hanno passato questo script che si occupa in maniera abbastanza efficiente di validare un form con js.
io vorrei renderlo ancora piu efficiente ovvero popolare il vettore "validations" dinamicamente quando l'utente clicca sul pulsante "invia" in modo tale che questo script valga indistintamente per tutti i form.
Non sono pratico di js, qualcuno ha un idea di come fare?

codice:
<script type="text/javascript">
<!--
var validations = new Array();
// Define which validations to perform. Each array item
// holds the form field to validate, and the validation
// to be applied. This is the only part you need to
// customize in order to use the script in a new page!
validations[0]=["document.myform.username", "notblank"];
validations[1]=["document.myform.useremail", "validemail"];
validations[2]=["document.myform.favoritenumber", "isnumber"];



// Customize above array when used with a new page.

function isEmpty(s)
{
  if (s == null || s.length == 0)
    return true;
  // The test returns true if there is at least one non-
  // whitespace, meaning the string is not empty. If the
  // test returns true, the string is empty.
  return !/\S/.test(s);
}



function looksLikeEmail(field)
{
  var s = field.value;
  if (isEmpty(s))
   {
     alert("Email may not be empty");
     field.focus();
     return false;
   }



  if (/[^@]+@\w+/.test(s))
       return true;
  alert("E-mail not in valid form.");
  field.focus();
  return false;
}



function isInteger(field)

{

  var s = field.value;

  if (isEmpty(s))

   {

     alert("Field cannot be empty");

     field.focus();

     return false;

   }



  if (!(/^-?\d+$/.test(s)))

   {

     alert("Field must contain only digits");

     field.focus();

     return false;

   }

 return true;

}



function validate()

{

  var i;

  var checkToMake;

  var field;



  for (i = 0; i < validations.length; i++)

   {

     field = eval(validations[i][0]);

     checkToMake = validations[i][1];

     switch (checkToMake)

      {

       case 'notblank': if (isEmpty(field.value))

                          {

                           alert("Field may not be empty");

                           field.focus();

                           return false;

                          }

                        break;

       case 'validemail':  if (!looksLikeEmail(field))

                               return false;

                           break;

       case 'isnumber':  if (!isInteger(field))

                            return false;

      }

   }

  return true;

}

//-->

</script>