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;
        }
Catch us on social media

Author Profile

Deependra Kushwah
Deependra Kushwah
Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Deependra Kushwah

Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Leave a Reply

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

RSS
Facebook
Google+
https://betechnical.blog/2016/06/11/html-form-validation-using-one-function-of-javascript">
Twitter
YouTube
Pinterest
Pinterest
LinkedIn
Instagram
%d bloggers like this: