Options
| Option | HTML attribute | Type | Description |
|---|---|---|---|
message | data-fv-color-message | String | The error message |
type | data-fv-color-type | String|String[] | The type of color. It can be:
|
When setting options via HTML attributes, remember to enable the validator by setting data-fv-color="true".
The message and other options can be updated on the fly via the updateMessage() and updateOption() methods
Supported types
Following is the list of supported types:
The keyword type accepts the following colors:
| Starting with | Colors |
|---|---|
| A | |
| B | |
| C | |
| D | |
| F | |
| G | |
| H | |
| I | |
| K | |
| L | |
| M | |
| N | |
| O | |
| P | |
| R | |
| S | |
| T | |
| V | |
| W | |
| Y | |
Example
Basic example
<form id="colorForm" class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Color</label>
<div class="col-xs-6">
<input type="text" class="form-control" name="color" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#colorForm').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
color: {
validators: {
color: {
type: ['hex', 'hsl', 'hsla', 'keyword', 'rgb', 'rgba'], // The default value for type
message: 'The value is not valid color'
}
}
}
}
});
});
</script><form id="colorForm" class="form-horizontal"
data-fv-framework="bootstrap"
data-fv-icon-valid="glyphicon glyphicon-ok"
data-fv-icon-invalid="glyphicon glyphicon-remove"
data-fv-icon-validating="glyphicon glyphicon-refresh">
<div class="form-group">
<label class="col-sm-3 control-label">Color</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="color"
data-fv-color="true"
data-fv-color-type="hex,hsl,hsla,keyword,rgb,rgba"
data-fv-color-message="The value is not valid color" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#colorForm').formValidation();
});
</script>HTML5 example
The color validator will be turned on automatically if the input uses HTML 5 type="color" attribute.
According to the W3C specification, the color input only accepts 6 hex character values. 3 hex character values as #FFF is not valid.
<form id="colorHtml5Form" class="form-horizontal"
data-fv-framework="bootstrap"
data-fv-icon-valid="glyphicon glyphicon-ok"
data-fv-icon-invalid="glyphicon glyphicon-remove"
data-fv-icon-validating="glyphicon glyphicon-refresh">
<div class="form-group">
<label class="col-xs-3 control-label">Color</label>
<div class="col-xs-5">
<input class="form-control" name="color"
type="color"
data-fv-color-message="The color code is not valid" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#colorHtml5Form').formValidation();
});
</script>Color picker example
The following form uses a Bootstrap Color Picker.
<!-- 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>
<style type="text/css">
/* Adjust feedback icon position */
#colorPickerForm .colorPickerContainer .form-control-feedback {
right: -15px;
}
</style>
<form id="colorPickerForm" class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Color</label>
<div class="col-xs-6 colorPickerContainer">
<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>
<script>
$(document).ready(function() {
$('#colorPickerForm').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
color: {
validators: {
color: {
message: 'The color code is not valid'
}
}
}
}
});
$('#colorPicker')
.colorpicker()
.on('showPicker changeColor', function(e) {
$('#colorPickerForm').formValidation('revalidateField', 'color');
});
});
</script>Related validators
The following validators might be useful to you: