浏览代码

Update document: Add "Write new validator" section

nghuuphuoc 12 年之前
父节点
当前提交
8d85119f14
共有 6 个文件被更改,包括 57 次插入10 次删除
  1. 6 6
      CHANGELOG.md
  2. 47 0
      README.md
  3. 1 1
      dist/css/bootstrapValidator.min.css
  4. 1 1
      dist/js/bootstrapValidator.js
  5. 1 1
      dist/js/bootstrapValidator.min.js
  6. 1 1
      package.json

+ 6 - 6
CHANGELOG.md

@@ -1,12 +1,12 @@
 # Change Log
 
-v0.2.0
-* Add custom submit handler (using ```submitHandler``` option)
-* Add ```live``` option
-* Add ```callback``` validator
-* Add ```creditCard``` validator
-* Add ```different``` validator
+v0.2.0 (2013-10-21)
+* #9: Add ```creditCard``` validator
+* #18: Add ```different``` validator
+* #20: Add custom submit handler (using ```submitHandler``` option)
+* #21: Add ```callback``` validator
 * #22: Support form that labels are placed in extra small (col-xs-), small (col-sm-), medium (col-md-) elements
+* #24: Add ```live``` option
 * #25: The ```regexp``` validator does not work
 
 v0.1.1 (2013-10-17)

+ 47 - 0
README.md

@@ -6,6 +6,7 @@ A jQuery plugin to validate form fields. Use with Bootstrap 3
 
 * Built from scratch
 * Many built-in [validators](#validators)
+* It is easy to [write new validator](#write-new-validator)
 
 ## Required
 
@@ -202,6 +203,52 @@ You can use [bower](http://bower.io) to install BootstrapValidator:
 $ bower install bootstrapValidator
 ```
 
+## Write new validator
+
+A validator have to follow the syntax:
+
+```javascript
+(function($) {
+    $.fn.bootstrapValidator.validators.<validatorName> = {
+        /**
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field The jQuery object represents the field element
+         * @param {Object} options The validator options
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+            // You can get the field value
+            // var value = $field.val();
+            //
+            // Perform validating
+            // ...
+            //
+            // return true if the field value is valid
+            // otherwise return false
+        }
+    };
+}(window.jQuery));
+```
+
+```<validatorName>``` is the validator name.
+Since the validators are distinct by the names, ```<validatorName>``` has to be different with [built-in validators](#validators)
+
+To apply the validator to particular field:
+
+```javascript
+$(form).bootstrapValidator({
+    fields: {
+        <fieldName>: {
+            // Replace <validatorName> with the name of validator
+            // <validatorOptions> will be passed as third parameter of the validate(validator, $field, options) method
+            <validatorName>: <validatorOptions>
+        }
+    }
+});
+```
+
+You can look at the source of built-in validators in [```src/js/validator```](src/js/validator) directory.
+
 ## Build
 
 BootstrapValidator uses [grunt](http://gruntjs.com) to simplify building process.

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -1,5 +1,5 @@
 /**
- * BootstrapValidator v0.2.0-dev (http://github.com/nghuuphuoc/bootstrapvalidator)
+ * BootstrapValidator v0.2.0 (http://github.com/nghuuphuoc/bootstrapvalidator)
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *

+ 1 - 1
dist/js/bootstrapValidator.js

@@ -1,5 +1,5 @@
 /**
- * BootstrapValidator v0.2.0-dev (http://github.com/nghuuphuoc/bootstrapvalidator)
+ * BootstrapValidator v0.2.0 (http://github.com/nghuuphuoc/bootstrapvalidator)
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *

+ 1 - 1
dist/js/bootstrapValidator.min.js

@@ -1,5 +1,5 @@
 /**
- * BootstrapValidator v0.2.0-dev (http://github.com/nghuuphuoc/bootstrapvalidator)
+ * BootstrapValidator v0.2.0 (http://github.com/nghuuphuoc/bootstrapvalidator)
  *
  * A jQuery plugin to validate form fields. Use with Bootstrap 3
  *

+ 1 - 1
package.json

@@ -13,5 +13,5 @@
     "grunt-contrib-cssmin": "~0.6.2",
     "grunt-contrib-copy": "~0.4.1"
   },
-  "version": "0.2.0-dev"
+  "version": "0.2.0"
 }