Check all elements in form with Javascript

前端 未结 4 1039
庸人自扰
庸人自扰 2021-02-04 20:41

I know javascript in the beginning level, but I have a problem.

I have 7 input elements in a form and I want all of them to be filled. I came up with t

4条回答
  •  别跟我提以往
    2021-02-04 20:50

    This is the simple and dirty way.

    A better way is to update a validation message that the fields are required.

    function validateForm()
    {
      var fields = ["name, phone", "compname", "mail", "compphone", "adres", "zip"]
    
      var i, l = fields.length;
      var fieldname;
      for (i = 0; i < l; i++) {
        fieldname = fields[i];
        if (document.forms["register"][fieldname].value === "") {
          alert(fieldname + " can not be empty");
          return false;
        }
      }
      return true;
    }
    

提交回复
热议问题