Validators / hexColor

Validate a hex color.

Options

Option HTML attribute Type Description
message data-bv-hexcolor-message String The error message

Example

Basic

<form id="colorForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-lg-3 control-label">Color</label>
        <div class="col-lg-3">
            <input type="text" class="form-control" name="color" />
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#colorForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            color: {
                validators: {
                    hexColor: {
                        message: 'The color code is not valid'
                    }
                }
            }
        }
    });
});

Color picker

The following form uses a Bootstrap Color Picker.

Try to type an invalid color code and choose the picker. The picker automatically fixes or reset the color, and the error message is gone.

Pick a color
<!-- Required Bootstrap ColorPicker library -->
<link href="/vendor/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css" rel="stylesheet" />
<script src="/vendor/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js"></script>

<form id="colorPickerForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-lg-3 control-label">Color</label>
        <div class="col-lg-4">
            <div class="input-group" id="colorPicker">
                <input type="text" class="form-control" name="color" />
                <span class="input-group-addon" style="color: #fff">Pick a color</span>
            </div>
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#colorPickerForm').bootstrapValidator({
        fields: {
            color: {
                validators: {
                    hexColor: {
                        message: 'The color code is not valid'
                    }
                }
            }
        }
    });

    $('#colorPicker')
        .colorpicker()
        .on('showPicker changeColor', function(e) {
            // Revalidate the color when user choose a color from the color picker
            $('#colorPickerForm').bootstrapValidator('revalidateField', 'color');
        });
});

The following validators might be useful to you: