Monday, May 4, 2009

Validate All TextBoxes Using JavaScript

Using following function you can find out all text boxes in the page are entered a value or not, no need to manually write each text box name to validate.

<script language="javascript" type="text/javascript">
function validate() {
    // Get all input controls
    var arrTextBox = document.getElementsByTagName("input");
    var retValue = 1;
    // Loop through all controls
    for(i=0;i<arrTextBox.length;i++)
    {
        // Check whether control is textbox and has a value
        if (arrTextBox[i].type == "text" && arrTextBox[i].value == "") {
            retValue = 0;
        }
    }
    //based on the validation return the value
    if (retValue == 0) {
        alert("Validation Failed");
        return false;
    }
    else {
        alert("Validation Success");
        return true;
    }
}
<script>


Tags : Validate JavaScript, All TextBoxes, document.getElementByTagName

No comments:

Post a Comment