Form Validation

jqBootstrapValidation - A JQuery validation plugin for bootstrap forms

Official jqBootstrapValidation documentation

Email address


                                                
  1. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Email address</label>
  4. <divclass="controls">
  5. <inputtype="email"/>
  6. <pclass="help-block"></p>
  7. </div>
  8. </div>
  9. </form>

                                            
Attribute Value Req?
type email
data-validation-email-message (your failure message)

Required


                                                
  1. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <inputtype="text"name="some_field"required/>
  6. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type a number</label>
  4. <divclass="controls">
  5. <inputtype="number"min="42"name="some_field"/>
  6. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type a number</label>
  4. <divclass="controls">
  5. <inputtype="number"max="42"name="some_field"/>
  6. <pclass="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. <formclass="form-horizontal"novalidate>
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <inputtype="text"maxlength="10"name="some_field"/>
  6. <pclass="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. <formclass="form-horizontal"novalidate>
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <inputtype="text"minlength="10"name="some_field"/>
  6. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <inputtype="text"
  6. pattern="a.*z"
  7. data-validation-pattern-message="Must start with 'a' and end with 'z'"
  8. name="some_field"
  9. />
  10. <pclass="help-block">Must start with 'a' and end with 'z'</p>
  11. </div>
  12. </div>
  13. </form>

                                            
Attribute Value Req?
pattern Regexto determine if the input is acceptable
Noteonly expects abc, not /^abc$/ig
data-validation-pattern-message (your failure message)

Confirm match


                                                
  1. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <inputtype="text"name="some_field"/>
  6. <pclass="help-block"></p>
  7. </div>
  8. </div>
  9. <divclass="control-group">
  10. <labelclass="control-label">Type it again</label>
  11. <divclass="controls">
  12. <input
  13. type="text"
  14. data-validation-match-match="some_field"
  15. name="some_other_field"
  16. />
  17. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Select your two favourite colours</label>
  4. <divclass="controls">
  5. <labelclass="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. <labelclass="checkbox">
  14. <inputtype="checkbox"name="some_field"/>Orange
  15. </label>
  16. <labelclass="checkbox">
  17. <inputtype="checkbox"name="some_field"/>Yellow
  18. </label>
  19. <labelclass="checkbox">
  20. <inputtype="checkbox"name="some_field"/>Green
  21. </label>
  22. <labelclass="checkbox">
  23. <inputtype="checkbox"name="some_field"/>Blue
  24. </label>
  25. <labelclass="checkbox">
  26. <inputtype="checkbox"name="some_field"/>Indigo
  27. </label>
  28. <labelclass="checkbox">
  29. <inputtype="checkbox"name="some_field"/>Violet
  30. </label>
  31. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Select your two favourite colours</label>
  4. <divclass="controls">
  5. <labelclass="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. <labelclass="checkbox">
  14. <inputtype="checkbox"name="some_field"/>Orange
  15. </label>
  16. <labelclass="checkbox">
  17. <inputtype="checkbox"name="some_field"/>Yellow
  18. </label>
  19. <labelclass="checkbox">
  20. <inputtype="checkbox"name="some_field"/>Green
  21. </label>
  22. <labelclass="checkbox">
  23. <inputtype="checkbox"name="some_field"/>Blue
  24. </label>
  25. <labelclass="checkbox">
  26. <inputtype="checkbox"name="some_field"/>Indigo
  27. </label>
  28. <labelclass="checkbox">
  29. <inputtype="checkbox"name="some_field"/>Violet
  30. </label>
  31. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Select your two favourite colours</label>
  4. <divclass="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. <pclass="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. functiondemo_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. <formclass="form-horizontal">
  11. <divclass="control-group">
  12. <labelclass="control-label">Type something</label>
  13. <divclass="controls">
  14. <input
  15. type="text"
  16. data-validation-callback-callback="demo_callback_function"
  17. />
  18. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="controls">
  5. <input
  6. type="text"
  7. name="ajax_input"
  8. data-validation-ajax-ajax="/path/to/validator/script"
  9. />
  10. <pclass="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. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type a number</label>
  4. <divclass="controls">
  5. <inputtype="number"/>
  6. <pclass="help-block"></p>
  7. </div>
  8. </div>
  9. <divclass="form-actions">
  10. </form>

                                            
Attribute Value Req?
type "number"
data-validation-number-message your failure message

Mix and multiply validators


                                                
  1. <formclass="form-horizontal">
  2. <divclass="control-group">
  3. <labelclass="control-label">Type something</label>
  4. <divclass="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. <pclass="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=10to use the maxlengthHTML5 validator, for example.