Hi guys,

A quick problem that I'm facing with a return method.
I've created a page for a friend with a questionnaire and i am validating all the radio buttons, this works nicely.

When i want to verify the input fields of text type, it alerts me but the return false; it's not kicking in.... don't know why.

<script type='text/javascript'>
window.onload = function() {
    document.getElementById("trimite").onclick = function(e) {      

            var checked = $("#container :radio:checked");
            var groups = [];
            $("#container :radio").each(function() {
                if (groups.indexOf(this.name) < 0) {
                    groups.push(this.name);
                }
            });
            if (groups.length == checked.length) {
                alert('Multumim ca ati raspuns la acest chestionar!');                               

                // here comes the problem, this return false it's not working

                 $('#chestionar input[type=text]').each(function(n,element){  
                    if ($(element).val()=='') {  
                      alert('Trebuie sa complectati campul '+element.id);  
                      return false;  
                    }  
                  });                
            }
            else {
                var total = groups.length - checked.length;
                alert('Nu ati raspuns la ' + total + ' intrebari!');
                return false;
            }
    };  
};
</script>

Recommended Answers

All 2 Replies

Even though I don't use JQuery, the syntax for line 17 looks odd. The each() function could be applied to correct tag name, but not an element name or id? Am I wrong? You are attempting to go through elements with name/id "chestionar"?

PS: Checking value with =='' allows a loop hole. If a user enter at least 1 white space character in it, it will pass the validation. You should use .val().replace(/\s+/,"").length()==0 instead.

Thank you, done it in another way, using the replace also :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.