Html Form validation using one function of javascript

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

<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;
        }
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp
Share on email

Leave a Reply

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

Recommended for you
Connect With Us
Stay Connected
Subscribe to our newsletter today to receive updates on latest tech news, tutorials SEO tips many more.
Latest Articles