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;
        }
Close

About The Author

Deependra Kushwah
Deependra kushwah is a member of the fastest growing bloggers community "betechnical", Author, Youtuber, and hardcore Coder. I love writing code in different languages, I also write blogs on tech tutorials, gadgets review and also post some technical videos on youtube on many topics.

This site uses Akismet to reduce spam. Learn how your comment data is processed.