Validators / regexp

Check if the value matches given Javascript regular expression.

Options

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
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:

1 — 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.

2 — Does the pattern work with external services?

You can use the following services to test the regular expression:

Example

In the following form, user is asked to enter the full name which alphabetical characters and spaces only.

<form id="profileForm" class="form-horizontal">
    <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" />
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#profileForm').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">
    <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"
                pattern="^[a-z\s]+$"
                data-bv-regexp-message="The full name can consist of alphabetical characters and spaces only" />
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#profileForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }
    });
});