在Bootstrap中更改表单样式以进行验证

时间:2016-09-05 05:07:13

标签: javascript jquery html css twitter-bootstrap

我发现了一个代码片段,它正是我需要的,只需稍加改动。 The sample code is here,我尝试创建一个可以访问here的示例表单。 我需要的唯一调整是,我想在加载表单后显示必填字段的星号。但截至目前,它显示了错误样式(页面打开时不需要)。我该如何更改表格?

脚本

    $(document).ready(function() {
    $('.input-group input[required], .input-group textarea[required], .input-group select[required]').on('keyup change', function() {
        var $form = $(this).closest('form'),
            $group = $(this).closest('.input-group'),
            $addon = $group.find('.input-group-addon'),
            $icon = $addon.find('span'),
            state = false;

        if (!$group.data('validate')) {
            state = $(this).val() ? true : false;
        } else if ($group.data('validate') == "email") {
            state = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($(this).val())
        } else if ($group.data('validate') == 'phone') {
            state = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/.test($(this).val())
        } else if ($group.data('validate') == "length") {
            state = $(this).val().length >= $group.data('length') ? true : false;
        } else if ($group.data('validate') == "number") {
            state = !isNaN(parseFloat($(this).val())) && isFinite($(this).val());
        }

        if (state) {
            $addon.removeClass('danger');
            $addon.addClass('success');
            $icon.attr('class', 'glyphicon glyphicon-ok');
        } else {
            $addon.removeClass('success');
            $addon.addClass('danger');
            $icon.attr('class', 'glyphicon glyphicon-remove');
        }

        if ($form.find('.input-group-addon.danger').length == 0) {
            $form.find('[type="submit"]').prop('disabled', false);
        } else {
            $form.find('[type="submit"]').prop('disabled', true);
        }
    });

    $('.input-group input[required], .input-group textarea[required], .input-group select[required]').trigger('change');
});

HTML

 <div class="container">
   <div class="row">
      <h2>Input Validation + Colorful Input Groups</h2>
   </div>
   <div class="row">
      <div class="col-sm-offset-4 col-sm-4">
         <form method="post">
            <div class="form-group">
               <label for="validate-text">Validate Text</label>
               <div class="input-group">
                  <input type="text" class="form-control" name="validate-text" id="validate-text" placeholder="Validate Text" required>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-optional">Optional</label>
               <div class="input-group">
                  <input type="text" class="form-control" name="validate-optional" id="validate-optional" placeholder="Optional">
                  <span class="input-group-addon info"><span class="glyphicon glyphicon-asterisk"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-optional">Already Validated!</label>
               <div class="input-group">
                  <input type="text" class="form-control" name="validate-text" id="validate-text" placeholder="Validate Text" value="Validated!" required>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-email">Validate Email</label>
               <div class="input-group" data-validate="email">
                  <input type="text" class="form-control" name="validate-email" id="validate-email" placeholder="Validate Email" required>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-phone">Validate Phone</label>
               <div class="input-group" data-validate="phone">
                  <input type="text" class="form-control" name="validate-phone" id="validate-phone" placeholder="(814) 555-1234" required>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-length">Minimum Length</label>
               <div class="input-group" data-validate="length" data-length="5">
                  <textarea type="text" class="form-control" name="validate-length" id="validate-length" placeholder="Validate Length" required></textarea>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-select">Validate Select</label>
               <div class="input-group">
                  <select class="form-control" name="validate-select" id="validate-select" placeholder="Validate Select" required>
                     <option value="">Select an item</option>
                     <option value="item_1">Item 1</option>
                     <option value="item_2">Item 2</option>
                     <option value="item_3">Item 3</option>
                  </select>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <div class="form-group">
               <label for="validate-number">Validate Number</label>
               <div class="input-group" data-validate="number">
                  <input type="text" class="form-control" name="validate-number" id="validate-number" placeholder="Validate Number" required>
                  <span class="input-group-addon danger"><span class="glyphicon glyphicon-remove"></span></span>
               </div>
            </div>
            <button type="submit" class="btn btn-primary col-xs-12" disabled>Submit</button>
         </form>
      </div>
   </div>
</div>

1 个答案:

答案 0 :(得分:1)

您需要在脚本中注释以下行。

$('.input-group input[required], .input-group textarea[required], .input-group select[required]').trigger('change');

这是document.ready的最后一行。这是负责在页面加载时触发更改事件并将星号更改为交叉的行。只需评论一下这条线就可以了。

修改 添加danger类而不是info类;默认情况下,这将以红色显示您的星号,在验证时,它将获得带有复选标记的绿色背景。即,修改星号表格组如下。                              可选的                                                                                         

注意 撤消我之前建议的脚本更改。