Html Form validation using one function of javascript

<script type="text/javascript">
  function validateform() 
        {
            var txtcontrols = document.getElementsByTagName("input");
            for (var count = 0; count &lt; txtcontrols.length; count++) {
                if (txtcontrols[count].value == '') {
                    errmsg.innerHTML = "Please Enter " + txtcontrols[count].name;
                    return false;
                }
                if (txtcontrols[count].getAttribute('type') == 'radio') {
                    var counter = 0;
                    var radiobuttons = document.getElementsByName(txtcontrols[count].name);
                    for (var radio = 0; radio &lt; radiobuttons.length; radio++) {
                        if (radiobuttons[radio].checked == false) {
                            counter++;
                        }
                    }
                    if (radiobuttons.length == counter) {
                        errmsg.innerHTML = "Please Select " + txtcontrols[count].name;
                        return false;
                    }
                }

                if (txtcontrols[count].getAttribute('type') == 'checkbox') {
                    var counter = 0;
                    var checkboxes = document.getElementsByName(txtcontrols[count].name);

                    for (var checkbox = 0; checkbox &lt; checkboxes.length; checkbox++) {
                        if (checkboxes[checkbox].checked == false) {
                            counter++;
                        }

                    }
                    if (counter == checkboxes.length) {
                        errmsg.innerHTML = "Please Select " + txtcontrols[count].name;
                        return false;
                    }
                }
            }
            return true;
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s