v0.5.3 is released on 2014.11.05!

color validator

Validate a color in different formats

Validators

Improve it on Github

Options

Option HTML attribute Type Description
message data-bv-color-message String The error message
type data-bv-color-type String|String[]

The type of color. It can be:

  • One of supported types listed below
  • Array of supported types
  • A string consists of supported types, separated by a comma
When setting options via HTML attributes, remember to enable the validator by setting data-bv-color="true".

Supported types

Following is the list of supported types:

Type Valid sample Invalid sample
hex
hsl
hsla
keyword
rgb
rgba

The keyword type accepts the following colors:

Starting with Colors
A
aliceblue, antiquewhite, aqua, aquamarine, azure
B
beige, bisque, black, blanchedalmond, blue, blueviolet, brown, burlywood
C
cadetblue, chartreuse, chocolate, coral, cornflowerblue, cornsilk, crimson, cyan
D
darkblue, darkcyan, darkgoldenrod, darkgray, darkgreen, darkgrey, darkkhaki, darkmagenta,
darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue,
darkslategray, darkslategrey, darkturquoise, darkviolet, deeppink, deepskyblue, dimgray,
dimgrey, dodgerblue
F
firebrick, floralwhite, forestgreen, fuchsia
G
gainsboro, ghostwhite, gold, goldenrod, gray, green, greenyellow, grey
H
honeydew, hotpink
I
indianred, indigo, ivory
K
khaki
L
lavender, lavenderblush, lawngreen, lemonchiffon, lightblue, lightcoral, lightcyan,
lightgoldenrodyellow, lightgray, lightgreen, lightgrey, lightpink, lightsalmon, lightseagreen,
lightskyblue, lightslategray, lightslategrey, lightsteelblue, lightyellow, lime, limegreen,
linen
M
magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple, mediumseagreen,
mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream,
mistyrose, moccasin
N
navajowhite, navy
O
oldlace, olive, olivedrab, orange, orangered, orchid
P
palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink,
plum, powderblue, purple
R
red, rosybrown, royalblue
S
saddlebrown, salmon, sandybrown, seagreen, seashell, sienna, silver, skyblue, slateblue,
slategray, slategrey, snow, springgreen, steelblue
T
tan, teal, thistle, tomato, transparent, turquoise
V
violet
W
wheat, white, whitesmoke
Y
yellow, yellowgreen

Example

<form id="colorForm" class="form-horizontal">
    <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" />
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#colorForm').bootstrapValidator({
        feedbackIcons: {
            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'
                    }
                }
            }
        }
    });
});
<form id="colorForm" class="form-horizontal"
    data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
    data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
    data-bv-feedbackicons-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-bv-color="true"
                data-bv-color-type="hex,hsl,hsla,keyword,rgb,rgba"
                data-bv-color-message="The value is not valid color" />
        </div>
    </div>
</form>

<script>
$(document).ready(function() {
    $('#colorForm').bootstrapValidator();
});
</script>

Related validators

The following validators might be useful to you: