0

How to using loop for with javascript [checkform function] ?

<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
  if (form.text_0.value == "") {
    alert( "Please enter text" );
    return false ;
  }
return true ;
}
</script>

I try to using loop for like this

<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
  var y;
  for (y = 0; y < 10; y++) {
      if (form.text_'y'.value == "") {
        alert( "Please enter text" );
        return false ;
      }
  }
return true ;
}
</script>

But not work , how can i do that ?

1 Answers1

1

You can use the [] operator:

  if (form["text_" + y].value == "") {
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Not work, thank `` – robert wisyert Nov 30 '14 at 16:01
  • @robertwisyert [yes it does - here is a working jsfiddle](http://jsfiddle.net/63phjrrd/) – Pointy Nov 30 '14 at 16:07