Options
* — Required option
Option | HTML attribute | Type | Description |
---|---|---|---|
message |
data-fv-regexp-message |
String | The error message |
regexp * |
data-fv-regexp-regexp or pattern |
String | The Javascript regular expression |
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:
Useful patterns
The following table collects some useful patterns:
Description | Pattern |
---|---|
SSN (Social Security Numbers) | ^(?!000|666)(?:[0-6][0-9]{2}|7(?:[0-6][0-9]|7[0-2]))-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$
|
Traditional time in 12-hour format |
|
Traditional time in 24-hour format |
|
Numbers in a particular range |
|
ejemplos
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-xs-3 control-label">Full name</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="fullName" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#regexpForm').formValidation({
framework: 'bootstrap',
icon: {
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'
}
}
}
}
});
});
</script>
<form id="regexpForm" 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">Full name</label>
<div class="col-xs-5">
<input type="text" class="form-control" name="fullName"
data-fv-regexp="true"
data-fv-regexp-regexp="^[a-z\s]+$"
data-fv-regexp-message="The full name can consist of alphabetical characters and spaces only" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#regexpForm').formValidation();
});
</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-fv-regexp="false"
.
<form id="meetingForm" 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">Meeting time</label>
<div class="col-xs-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-fv-regexp-message="The meeting time must be between 09:00 and 17:59" />
</div>
</div>
</form>
<script>
$(document).ready(function() {
$('#meetingForm').formValidation();
});
</script>
Validating social account URL
Instead of using the uri validator, this example uses the
regexp validator to validate Facebook account URL, such as
http(s)://facebook.com/account
: