开发者

Javascript - return false error

I have a form validation function that loops through the form elements checking for empty fields - code below:

function formValidate(ourform){
  var formelems = document.getElementById(ourform).elements;

  for (var i=0; i<formelems.length; i++){
  if(formelems[i].value == "") {
   alert("Empty Field(s). Please fill in all the fields.");
   return false;
    }   
  }    
}

the problem is that my form does not return false if there's an em开发者_开发百科pty field - it processes the form. I have solved this before but now i just cannot remember how.

Thanks.


Basically the answer is that in your example, you aren't actually returning anything. In Pandiya's example he is returning true/false depending on the conditions. Remember to also use a return statement in whatever even call you are using, probably in your example it would be

<form blah blah blah onsubmit="return formValidate(this)">


Hai try this,

function checkForEmpties(form) {
var i;
var formElements = form.elements;


for (i=0; i<formElements.length; i++) 
    {
    if (formElements[i].type == "text") {
        if (formElements[i].value != "") {
            return true;
            break;
        }
    }
    else if (formElements[i].type == "checkbox") {
        if (formElements[i].checked) {
            return true;
            break;
        }
    } 

}
return false;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜