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

时间:2009-09-22 01:32:21

标签: javascript jquery validation

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

表单字段为:

类型,状态,评级,cterms,companyid,公司,姓名,地址,郊区,城市,州,国家,电影,电话,电子邮件和评论

如果有人可以帮我解决这个问题,那就太好了,我不介意它是用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>

4 个答案:

答案 0 :(得分:4)

这是一个快速模型:

$(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;
        }
     });
});

答案 1 :(得分:3)

function checkEmpty() {
       var empty = false;
       $("input").each(function() {
           empty = ($(this).val() == "") ? true : empty;
       });
       if(empty) {
           var empty_ok = confirm("Are you Ok with leaving stuff empty?");
           return empty_ok;
       }

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

答案 2 :(得分:1)

以表格形式循环遍历每个输入元素:

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

答案 3 :(得分:1)

(这就是为什么我在盲目回答之前要求加价)

我认为上面的任何解决方案都没有说明只有一个的复选框必须实际检查的逻辑,你不应该只是检查它们是否都有值或者它们是默认情况下都会有一个。这是我的版本,它依赖于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');
    }

});
});

Checkbox代码段:

  <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>

如果您有多个复选框组,则只有一个元素用.checkboxgroup类包围它们。