FormValidation v0.8.1 is released, supports Bootstrap 4 alpha 3

regexp validator

Check if the value matches given Javascript regular expression

Validators

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
When setting options via HTML attributes, remember to enable the validator by setting data-fv-regexp="true".
You don't need to do that when using HTML 5 pattern="..." attribute.
The message and other options can be updated on the fly via the updateMessage() and updateOption() methods

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:

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
  • without seconds (hh:mm): ^(1[0-2]|0?[1-9]):([0-5]?[0-9])$
  • with seconds (hh:mm:ss): ^(1[0-2]|0?[1-9]):([0-5]?[0-9]):([0-5]?[0-9])$
Traditional time in 24-hour format
  • without seconds (hh:mm): ^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$
  • with seconds (hh:mm:ss): ^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$
Numbers in a particular range
  • Range of 1-12 (hour, month): ^(1[0-2]|[1-9])$
  • Range of 1-24 (hour): ^(2[0-4]|1[0-9]|[1-9])$
  • Range of 0-59 (minute, second): ^[1-5]?[0-9]$
  • Range of 1-31 (day of month): ^(3[01]|[12][0-9]|[1-9])$
  • Range of 0-100 (percentage): ^(100|[1-9]?[0-9])$

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-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".

Looking at this example if you want to compare times
<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:

Example