如果所有字段都未完成,jquery/javascript警报

如果所有字段都未完成,jquery/javascript警报,javascript,jquery,validation,Javascript,Jquery,Validation,我有一个表单,如果所有字段都没有填写,我想提醒大家。我目前正在为必填字段使用jquery验证脚本,但我需要它来让人们知道,并不是所有字段都在保存前填写(它们不是必填字段,只是为了让他们知道表单尚未完成) 表单字段包括: 类型、状态、评级、字符、公司ID、公司、名称、姓氏、地址、郊区、城市、州、国家、pcode、电话、电子邮件和评论 如果有人能帮我解决这个问题,那就太好了,我不介意使用jquery或javascript,只是提醒不是所有字段都已完成,以及是否要继续保存 干杯 莱恩 编辑: 以下是表

我有一个表单,如果所有字段都没有填写,我想提醒大家。我目前正在为必填字段使用jquery验证脚本,但我需要它来让人们知道,并不是所有字段都在保存前填写(它们不是必填字段,只是为了让他们知道表单尚未完成)

表单字段包括:

类型、状态、评级、字符、公司ID、公司、名称、姓氏、地址、郊区、城市、州、国家、pcode、电话、电子邮件和评论

如果有人能帮我解决这个问题,那就太好了,我不介意使用jquery或javascript,只是提醒不是所有字段都已完成,以及是否要继续保存

干杯

莱恩

编辑:

以下是表格:

<form action="/index.php?option=com_database&view=add&Itemid=3&task=save" method="post" name="adminForm" id="adminForm">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="50%" align="left" valign="top"><table width="100%" border="0" cellspacing="2" cellpadding="3">
          <tr>
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong> </strong></td>
            <td></td>
          </tr>
          <tr>
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong>Active:</strong></td>
            <td><table border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td align="left" valign="middle"><input type="radio" name="published" id="published0" value="0" checked="checked" /></td>
                  <td align="left" valign="middle">&nbsp;</td>
                  <td align="left" valign="middle"><label for="published0">No</label>
                  </td>
                  <td align="left" valign="middle">&nbsp;</td>
                  <td align="left" valign="middle"><input type="radio" name="published" id="published1" value="1" /></td>
                  <td align="left" valign="middle">&nbsp;</td>
                  <td align="left" valign="middle"><label for="published1">Yes</label></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong>Date:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap" class="admintable"><input class="text_area" type="text" name="date" id="date" size="30" value="22/09/2009" disabled="disabled" style="width: 200px;" />
            </td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Type:</strong></td>
            <td><select name="type" id="type" size="1" class="validate[required]" style="width: 205px;">
                <option value="">- Select Type -</option>
                <option value="1">Customer</option>
                <option value="2">Supplier</option>
              </select></td>
          </tr>
          <tr class="admintable">
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong>Status:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><select name="status" id="status" size="1" class="validate[required]" style="width: 205px;">
                <option value="">- Select Status -</option>
                <option value="1">Existing</option>
                <option value="2">Potential</option>
              </select></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Rating:</strong></td>
            <td><select name="rating" id="rating" size="1" class="validate[required]" style="width: 205px;">
                <option value="">- Select Rating -</option>
                <option value="1">Principal</option>
                <option value="2">Secondary</option>
                <option value="3">Minor</option>
              </select></td>
          </tr>
          <tr class="admintable">
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong>Credit Terms:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><select name="cterms" id="cterms" size="1" class="validate[required]" style="width: 205px;">
                <option value="">- Select Credit Terms -</option>
                <option value="1">COD</option>
                <option value="2">30 Days</option>
                <option value="3">60 Days</option>
                <option value="4">90 Days</option>
              </select></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Database:</strong></td>
            <td><select name="companyid" id="companyid" size="1" class="validate[required]" style="width: 205px;">
                <option value="">- Select Database -</option>
                <option value="2">Cody Opal</option>
                <option value="1">National Opal Collection</option>
              </select></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Company:</strong></td>
            <td><input class="validate[required] text_area" type="text" name="company" id="company" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Address:</strong></td>
            <td><input class="validate[required] text_area" type="text" name="address" id="address" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Address 2:</strong></td>
            <td><input class="text_area" type="text" name="address2" id="address2" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Suburb:</strong></td>
            <td><input class="validate[required] text_area" type="text" name="suburb" id="suburb" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>City:</strong></td>
            <td><input class="validate[required] text_area" type="text" name="city" id="city" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>State:</strong></td>
            <td><div id="entry_state1">
                <input class="validate[required] text_area" type="text" name="state" id="state" size="30" value="" style="width: 200px;" />
              </div></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Postcode:</strong></td>
            <td><input class="validate[required] text_area" type="text" name="pcode" id="pcode" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Country:</strong></td>
            <td><select name="country" id="country" onChange="javascript: loadstate1();" class="selectstate validate[required]">
                <option value="">- Select Country -</option>
              </select></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Primary Contact Name:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="validate[required] text_area" type="text" name="name" id="name" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Primary Contact Surname:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" type="text" name="surname" id="surname" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Primary Contact Position:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" type="text" name="position" id="position" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Primary Contact Email:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="validate[required,custom[email]] text_area" type="text" name="email" id="email" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Secondary Contact:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" type="text" name="contact" id="contact" size="30" value="" style="width: 200px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Phone - Business:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="validate[required] text_area" name="country_code[]" type="text" id="country_code" value="+" size="2" maxlength="5" style="width: 33px;" />
              <input class="validate[required,custom[telephone]] text_area" type="text" name="phone_b" id="phone_b" size="22" value="" style="width: 160px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Phone - Direct:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" name="country_code[]" type="text" id="country_code" value="+" size="2" maxlength="5" style="width: 33px;" />
              <input class="validate[optional,custom[telephone]] text_area" type="text" name="phone_d" id="phone_d" size="22" value="" style="width: 160px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Phone - Mobile:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" name="country_code[]" type="text" id="country_code" value="+" size="2" maxlength="5" style="width: 33px;" />
              <input class="validate[optional,custom[telephone]] text_area" type="text" name="phone_m" id="phone_m" size="22" value="" style="width: 160px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="middle" nowrap="nowrap" class="key"><strong>Phone - Personal:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" name="country_code[]" type="text" id="country_code" value="+" size="2" maxlength="5" style="width: 33px;" />
              <input class="validate[optional,custom[telephone]] text_area" type="text" name="phone_p" id="phone_p" size="22" value="" style="width: 160px;" /></td>
          </tr>
          <tr>
            <td align="left" valign="middle" nowrap="nowrap" class="key"><strong>Fax:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><input class="text_area" name="country_code[]" type="text" id="country_code" value="+" size="2" maxlength="5" style="width: 33px;" />
              <input class="validate[optional,custom[telephone]] text_area" type="text" name="phone_f" id="phone_f" size="22" value="" style="width: 160px;" /></td>
          </tr>
          <tr>
            <td width="160" align="left" valign="top" nowrap="nowrap" class="key"><strong>Customer Comments:</strong></td>
            <td align="left" valign="middle" nowrap="nowrap"><textarea name="comments" id="comments" cols="30" rows="7"></textarea></td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="2" cellpadding="3">
          <tr>
            <td width="160" align="left" valign="top" nowrap="nowrap" class="key"><strong>Classifications: </strong></td>
            <td><div class="company_1">
                <table width="135" border="0" cellspacing="0" cellpadding="0" align="left" style="border: solid 1px #ff0000; margin-right: 10px; background-color: #ff0000; background-image: url(/templates/home/scripts/opacity.png);" class="classTables">
                  <tr>
                    <td colspan="2" align="center" valign="middle" style="background-color: #ff0000; background-image: none; padding: 2px 2px 2px 2px;"><strong>OTHER</strong></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[70]" id="classifications[70]" value="70" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[70]">VIP Client</label></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[71]" id="classifications[71]" value="71" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[71]">Retailer</label></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[72]" id="classifications[72]" value="72" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[72]">Media</label></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[73]" id="classifications[73]" value="73" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[73]">Consultant</label></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[74]" id="classifications[74]" value="74" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[74]">Contractor</label></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[75]" id="classifications[75]" value="75" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[75]">Other</label></td>
                  </tr>
                </table>
              </div></td>
          </tr>
        </table></td>
    </tr>
  </table>
  <input type="submit" name="submit" value="Add New Client" />
</form>


活动:
不
对
日期:
类型:
-选择类型-
顾客
供应商
状态:
-选择状态-
现有的
潜力
评级:
-选择评级-
校长
次要的
少数的
信贷条款:
-选择信用条款-
货到付款
30天
60天
90天
数据库:
-选择数据库-
科迪蛋白石
国家蛋白石收藏
公司:
地址:
地址2:
郊区:
城市:
状态:
邮政编码:
国家:
-选择国家-
主要联系人姓名:
主要联系人姓氏:
主要联系人位置:
主要联系人电子邮件:
第二联系人:
电话-商务:
电话-直拨:
手机-手机:
电话-个人:
传真:
客户评论:
分类:
其他
VIP客户
零售商
媒体
顾问
承包商
其他

在表单中循环每个输入元素:

$(':input', formName).each(function() {
    // Check for completion of each input type
})

如果单击
yes
,则确认返回true;如果单击
no
,则确认返回false。将该值作为传递给submit()事件的值返回给主验证器。

下面是一个快速模型:

$(document).ready(function() {
    $('form').submit(function() {
        var incomplete = $('form :input').filter(function() {
                             return $(this).val() == '';
                         });
        //if incomplete contains any elements, the form has not been filled 
        if(incomplete.length) {
            alert('please fill out the form');
            //to prevent submission of the form
            return false;
        }
     });
});
(这就是为什么我在盲目回答之前要求加价)

我认为上面的任何解决方案都不能解释这样一种逻辑:实际上只需要选中其中一个复选框,而不应该只检查它们是否都有值,或者默认情况下它们都有值。这是我的版本,它依赖于包含复选框的
div
来拥有一个“checkboxgroup”类

此外,未填充的元素将填充到“errorElements”数组中,您可以循环该数组并添加任何警告通知

$(function() {
$('#adminForm').submit( function ( event ) {
    var errorElements  = window.errorElements = [], valid = false;

    $(':input', this).not(':checkbox').each(function() {
    var val = $(this).val();
    if ( !val.length ) {
        valid = false;
        errorElements.push(this);
    }
    });

    $('.checkboxgroup', this).each(function() {
    var checkBoxes = $(':checkbox', this), oneChecked = false;
    checkBoxes.each(function() {
        if ( !oneChecked && !$(this).is(':checked') ) {
        valid = false;
        errorElements.push(this);
        } else {
        oneChecked = true;
        }
    });
    });

    if ( !errorElements.length ) {
    valid = true;
    }

    if ( !valid ) {
    event.preventDefault();
    alert('please fill in all form fields.');
    } else {
    alert('congratulations');
    }

});
});
复选框代码段:

  <div class="company_1 checkboxgroup">
                <table width="135" border="0" cellspacing="0" cellpadding="0" align="left" style="border: solid 1px #ff0000; margin-right: 10px; background-color: #ff0000; background-image: url(/templates/home/scripts/opacity.png);" class="classTables">
                  <tr>
                    <td colspan="2" align="center" valign="middle" style="background-color: #ff0000; background-image: none; padding: 2px 2px 2px 2px;"><strong>OTHER</strong></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[70]" id="classifications[70]" value="70" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[70]">VIP Client</label></td>

其他
VIP客户

如果有多个复选框组,则只需使用一个元素将其用.checkboxgroup类括起来即可。

该元素不会以子体为目标,尤其是在表单字段用分隔符括起来以符合样式的情况下。您正在查找
  <div class="company_1 checkboxgroup">
                <table width="135" border="0" cellspacing="0" cellpadding="0" align="left" style="border: solid 1px #ff0000; margin-right: 10px; background-color: #ff0000; background-image: url(/templates/home/scripts/opacity.png);" class="classTables">
                  <tr>
                    <td colspan="2" align="center" valign="middle" style="background-color: #ff0000; background-image: none; padding: 2px 2px 2px 2px;"><strong>OTHER</strong></td>
                  </tr>
                  <tr>
                    <td align="center" valign="middle" width="20"><input type="checkbox" name="classifications[70]" id="classifications[70]" value="70" class="checkbox" /></td>
                    <td align="left" valign="middle"><label for="classifications[70]">VIP Client</label></td>