views:

33

answers:

2

how to get all text box in loop for validation to check all

like

if (textbox.text == "",textbox2.text == "",textbox3.text == "")
{
textbox.Focus();
}
+1  A: 

Try using getElementsByTagName(elementType) or if you have given each textbox that you want to check a certain class, there are a few different implementations of getElementsByClass(className) floating around too (e.g. http://www.dustindiaz.com/getelementsbyclass/).

For instance if you all your text boxes are textareas:

var textboxes = document.getElementsByTagName('textarea');
for (var i=0; i<textboxes.length; i++){
    if (textboxes[i].value == ""){
        textboxes[i].focus();
        break;
    }
}

You could also replace 'document' with a specific parent element if you only want to check the textareas from inside a specific parent.

Edit: I added a break in the loop so it will exit after finding a text box that failed validation and focus on that element.

Rosco
A: 

Dun worry. You can do it within loop.

<script>
<!-- 
function chkForm(obj) 
{
      for (i=0; i<obj.childNodes.length; i++) 
    {
             if (obj.childNodes[i].tagName == "INPUT") 
        {
                        if (obj.childNodes[i].type == "text") 
            {
                         if(obj.childNodes[i].value == "")
                {
                    obj.childNodes[i].focus();  
                }
            }
          }   
    }
}
 -->
</script>



<form onsubmit="return chkForm(document.getElementById('myform'));" name="myform" id="myform">
<input type="text" name="myfield1"> <br>
<input type="text" name="myfield2"> <br>
<input type="submit" name="btnSubmit" value="Submit">
</form>
ppshein