v0.5.3 is released on 2014.11.05!
regexp validator
Check if the value matches given Javascript regular expression
Improve it on GithubOptions
Option | HTML attribute | Type | Description |
---|---|---|---|
message |
data-bv-regexp-message |
String | The error message |
regexp * |
data-bv-regexp-regexp or pattern |
String | The Javascript regular expression |
When setting options via HTML attributes, remember to enable the validator by setting data-bv-regexp="true".
You don't need to do that when using HTML 5 pattern="..." attribute.
You don't need to do that when using HTML 5 pattern="..." attribute.
Using a correct pattern
If the validator still pass when the field value doesn't match the pattern, please ensure you use a correct pattern.
Here are some check lists:
-
Is the pattern wrapped between
^
and$
?For example, if a field must be 5 digits number, then
^\d{5}
(no$
at the end) is wrong pattern.^\d{5}$
is right one. -
Does the pattern work with external services?
You can use the following services to test the regular expression:
Examples
In the following form, user is asked to enter the full name which alphabetical characters and spaces only.
Basic example
<form id="regexpForm" class="form-horizontal">
<div class="form-group">
<label class="col-sm-3 control-label">Full name</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="fullName" />
</div>
</div>
</form>
$(document).ready(function() {
$('#regexpForm').bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
fullName: {
validators: {
regexp: {
regexp: /^[a-z\s]+$/i,
message: 'The full name can consist of alphabetical characters and spaces only'
}
}
}
}
});
});
<form id="profileForm" 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-lg-3 control-label">Full name</label>
<div class="col-lg-4">
<input type="text" class="form-control" name="fullName"
data-bv-regexp="true"
data-bv-regexp-regexp="^[a-z\s]+$"
data-bv-regexp-message="The full name can consist of alphabetical characters and spaces only" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#profileForm').bootstrapValidator();
});
</script>
HTML 5 example
By default, the regexp validator will be used if the field uses HTML 5 pattern
attribute. In order to disable the validator, just simply set data-bv-regexp="false"
.
<form id="meetingForm" 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">Meeting time</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="time" placeholder="HH:mm"
pattern="^(09|1[0-7]{1}):[0-5]{1}[0-9]{1}$"
data-bv-regexp-message="The meeting time must be between 09:00 and 17:59" />
</div>
</div>
</form>
$(document).ready(function() {
$('#meetingForm').bootstrapValidator();
});