Form Validation

jqBootstrapValidation - A JQuery validation plugin for bootstrap forms

Official jqBootstrapValidation documentation

Email address

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Email address</label>
  4. <div class="controls">
  5. <input type="email" />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
type email
data-validation-email-message (your failure message)

Required

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input type="text" name="some_field" required />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
required (doesn't need a value)
data-validation-required-message (your failure message)

Minimum

Must be higher than 41

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type a number</label>
  4. <div class="controls">
  5. <input type="number" min="42" name="some_field" />
  6. <p class="help-block">Must be higher than 41</p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
min (minimum number to accept)
data-validation-min-message (your failure message)

Maximum

Must be lower than 43

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type a number</label>
  4. <div class="controls">
  5. <input type="number" max="42" name="some_field" />
  6. <p class="help-block">Must be lower than 43</p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
max (maximum number to accept)
data-validation-max-message (your failure message)

Maximum Length

  1. <form class="form-horizontal" novalidate>
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input type="text" maxlength="10" name="some_field" />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
maxlength (maximum number of characters to accept)
data-validation-maxlength-message (your failure message)

Minimum Length

  1. <form class="form-horizontal" novalidate>
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input type="text" minlength="10" name="some_field" />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. </form>
Attribute Value Req?
minlength (minimum number of characters to accept)
data-validation-minlength-message (your failure message)

Pattern

Must start with 'a' and end with 'z'

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input type="text"
  6. pattern="a.*z"
  7. data-validation-pattern-message="Must start with 'a' and end with 'z'"
  8. name="some_field"
  9. />
  10. <p class="help-block">Must start with 'a' and end with 'z'</p>
  11. </div>
  12. </div>
  13. </form>
Attribute Value Req?
pattern Regex siddim.com to determine if the input is acceptable
Note only expects abc, not /^abc$/ig
data-validation-pattern-message (your failure message)

Confirm match

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input type="text" name="some_field" />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. <div class="control-group">
  10. <label class="control-label">Type it again</label>
  11. <div class="controls">
  12. <input
  13. type="text"
  14. data-validation-match-match="some_field"
  15. name="some_other_field"
  16. />
  17. <p class="help-block"></p>
  18. </div>
  19. </div>
  20. </form>
Attribute Value Req?
data-validation-match-match name of form field to match
data-validation-match-message (your failure message)

Max checked options

Note: you only need to apply this to one checkbox; the form is searched for matching names for the rest.
  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Select your two favourite colours</label>
  4. <div class="controls">
  5. <label class="checkbox">
  6. <input
  7. type="checkbox"
  8. name="some_field"
  9. data-validation-maxchecked-maxchecked="2"
  10. data-validation-maxchecked-message="Don't be greedy!"
  11. /> Red
  12. </label>
  13. <label class="checkbox">
  14. <input type="checkbox" name="some_field" /> Orange
  15. </label>
  16. <label class="checkbox">
  17. <input type="checkbox" name="some_field" /> Yellow
  18. </label>
  19. <label class="checkbox">
  20. <input type="checkbox" name="some_field" /> Green
  21. </label>
  22. <label class="checkbox">
  23. <input type="checkbox" name="some_field" /> Blue
  24. </label>
  25. <label class="checkbox">
  26. <input type="checkbox" name="some_field" /> Indigo
  27. </label>
  28. <label class="checkbox">
  29. <input type="checkbox" name="some_field" /> Violet
  30. </label>
  31. <p class="help-block"></p>
  32. </div>
  33. </div>
  34. </form>
Attribute Value Req?
data-validation-maxchecked-maxchecked maximum number of options that may be checked
data-validation-maxchecked-message your failure message

Min checked options

Note: you only need to apply this to one checkbox; the form is searched for matching names for the rest.
  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Select your two favourite colours</label>
  4. <div class="controls">
  5. <label class="checkbox">
  6. <input
  7. type="checkbox"
  8. name="some_field"
  9. data-validation-minchecked-minchecked="2"
  10. data-validation-minchecked-message="Choose at least two"
  11. /> Red
  12. </label>
  13. <label class="checkbox">
  14. <input type="checkbox" name="some_field" /> Orange
  15. </label>
  16. <label class="checkbox">
  17. <input type="checkbox" name="some_field" /> Yellow
  18. </label>
  19. <label class="checkbox">
  20. <input type="checkbox" name="some_field" /> Green
  21. </label>
  22. <label class="checkbox">
  23. <input type="checkbox" name="some_field" /> Blue
  24. </label>
  25. <label class="checkbox">
  26. <input type="checkbox" name="some_field" /> Indigo
  27. </label>
  28. <label class="checkbox">
  29. <input type="checkbox" name="some_field" /> Violet
  30. </label>
  31. <p class="help-block"></p>
  32. </div>
  33. </div>
  34. </form>
Attribute Value Req?
data-validation-minchecked-minchecked minimum number of options that may be checked
data-validation-minchecked-message your failure message

Regex matching

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Select your two favourite colours</label>
  4. <div class="controls">
  5. <input
  6. type="text"
  7. data-validation-regex-regex="a.*z"
  8. data-validation-regex-message="Must start with 'a' and end with 'z'"
  9. />
  10. <p class="help-block"></p>
  11. </div>
  12. </div>
  13. </form>
Attribute Value Req?
data-validation-regex-regex pattern the input must match
data-validation-regex-message your failure message (strongly recommended you make this descriptive)

Javascript Callback

  1. <script>
  2. function demo_callback_function($el, value, callback) {
  3. callback({
  4. value: value,
  5. valid: /a.*z/.test(value),
  6. message: "Must start with 'a' and end with 'z'"
  7. });
  8. }
  9. </script>
  10. <form class="form-horizontal">
  11. <div class="control-group">
  12. <label class="control-label">Type something</label>
  13. <div class="controls">
  14. <input
  15. type="text"
  16. data-validation-callback-callback="demo_callback_function"
  17. />
  18. <p class="help-block"></p>
  19. </div>
  20. </div>
  21. </form>
Attribute Value Req?
data-validation-callback-callback name of the callback function
data-validation-callback-message your failure message
Note: You can include this in the callback response in stead, to customise against the user's error

The callback function will be passed the element (wrapped in jQuery), the field value, and a response function to call when ready.

The response function expects a single variable, a hash containing 'value', 'valid' and optionally 'message'.

The value must be returned, as this is checked against the current value and stale responses are ignored. The 'valid' item in the hash should evaluate to true only if the given value is valid. The message item in the hash, if given, will override the current message on the validator.

Sound a bit complex? Don't worry and take a look at the code tab, its pretty self explanatory.

Warning: While waiting for your function to use the callback, the validator assumes the field is valid. This is to guard against something failing server-side and stopping the user from submitting the form. If your callback takes a long time, the user could be able to submit the form with invalid data without realising.

AJAX

Note: As github doesn't offer dynamic pages yet, this form is only an example and accepts everything

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input
  6. type="text"
  7. name="ajax_input"
  8. data-validation-ajax-ajax="/path/to/validator/script"
  9. />
  10. <p class="help-block"></p>
  11. </div>
  12. </div>
  13. </form>
  1. <?php
  2. echo json_encode(
  3. array(
  4. "value" => $_REQUEST["value"],
  5. "valid" => preg_match("/^[A-Z].*$/", $_REQUEST["value"]),
  6. "message" => "Must start with an uppercase letter"
  7. )
  8. );
  1. {
  2. "value": "test",
  3. "valid": 0,
  4. "message": "Must start with an uppercase letter"
  5. }
Attribute Value Req?
data-validation-ajax-ajax path to the validation page on the server
data-validation-ajax-message your failure message
Note: You can include this in the ajax response in stead, to customise against the user's error

The ajax call will be passed the field name and the field value.

The response is expected as a JSON hash containing 'value', 'valid' and optionally 'message'.

Value must be returned, as this is checked against the current value so stale responses are ignored. The 'valid' item in the hash should evaluate to true only if the given value is valid. The message item in the hash, if given, will override the current message on the validator.

Sound a bit complex? Don't worry and take a look at the server tab, its pretty self explanatory.

Warning: While waiting for AJAX call, the validator assumes the field is valid. This is to guard against a something failing and stopping the user from submitting the form. If your ajax takes a long time, the user could be able to submit the form with invalid data without realising.

Numbers

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type a number</label>
  4. <div class="controls">
  5. <input type="number" />
  6. <p class="help-block"></p>
  7. </div>
  8. </div>
  9. <div class="form-actions">
  10. </form>
Attribute Value Req?
type "number"
data-validation-number-message your failure message

Mix and multiply validators

  1. <form class="form-horizontal">
  2. <div class="control-group">
  3. <label class="control-label">Type something</label>
  4. <div class="controls">
  5. <input
  6. type="text"
  7. data-validation-uppercase-regex="([^a-z]*[A-Z]*)*"
  8. data-validation-uppercase-message="Must be uppercase"
  9. data-validation-containsnumber-regex="([^0-9]*[0-9]+)+"
  10. data-validation-containsnumber-message="Needs at least one number"
  11. />
  12. <p class="help-block"></p>
  13. </div>
  14. </div>
  15. </form>

Have you noticed how all the validation messages data-attributes look the same?

data-validation-validatorname-message

Validators are attached with names, so you can have multiple of the same validator if you just name them differently.

In the form above, I've added two Regex validators, one requiring uppercase, the other requiring at least one number. Because they are named differently ('uppercase' and 'containsnumber' respectively) they are both applied rather than cancelling eachother out.

You can also use the HTML5 validators multiple times by specifying them in data-attribute format, such as data-validator-myvalidator-maxlength=10 to use the maxlength HTML5 validator, for example.