|
| 1 | +# jquery.input.validator [](https://travis-ci.org/creative-workflow/jquery.input.validator) [](https://codeclimate.com/github/creative-workflow/jquery.input.validator) |
| 2 | + |
| 3 | +This module helps to handle independent input validation based on standart html attributes. |
| 4 | + |
| 5 | +This plugin uses [jquery.debug](https://github.com/creative-workflow/jquery.debug) so all tracking information can be easily seen in the javascript console. |
| 6 | + |
| 7 | +## Installation |
| 8 | +```bash |
| 9 | + |
| 10 | +bower install jquery.input.validator |
| 11 | + |
| 12 | + # or |
| 13 | +npm install jquery.input.validator |
| 14 | + |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | +### javascript |
| 19 | +```js |
| 20 | +validator = $('body').inputValidator({ |
| 21 | + ... |
| 22 | +}); |
| 23 | + |
| 24 | +$('form').inputValidator().validate() |
| 25 | +$('form').inputValidator().reset() |
| 26 | + |
| 27 | +errors = validator.validateElement('<input type="email" value"invalid">') |
| 28 | +errors = validator.validateElement('<input type="number" value"invalid">') |
| 29 | +errors = validator.validateElement('<input type="tel" value"invalid">') |
| 30 | +errors = validator.validateElement('<input type="text" required>') |
| 31 | +errors = validator.validateElement('<input type="text" minlength="1" maxlength="3">') |
| 32 | + |
| 33 | +errors = validator.validateElement('<input type="text" data-rule-decimal="true">') |
| 34 | +errors = validator.validateElement('<input type="text" data-rule-pattern="true" pattern="blub[\\d*]" value="blub23" >') |
| 35 | +errors = validator.validateElement('<input type="text" data-has-class="hello" class="hello">') |
| 36 | + |
| 37 | +errors = validator.validateElement('<input type="text" required data-msg-required="required">') |
| 38 | + |
| 39 | +// TODO: write docs |
| 40 | +``` |
| 41 | +It also exposes the class `InputValidator` for manual instantiating. |
| 42 | + |
| 43 | +### Configuration |
| 44 | +#### |
| 45 | +```coffee |
| 46 | + validateOnFocusOut: true |
| 47 | + validateOnKeyUp: false |
| 48 | + validateOnClick: false |
| 49 | + |
| 50 | + focusInvalidElement: true |
| 51 | + removeHintOnFocus: false |
| 52 | + |
| 53 | + selectors: |
| 54 | + elements: 'input, textarea, select' |
| 55 | + ignore: ':hidden' |
| 56 | + |
| 57 | + classes: |
| 58 | + error: 'error' |
| 59 | + valid: 'valid' |
| 60 | + hint: 'error-hint' |
| 61 | + |
| 62 | + pattern: |
| 63 | + decimal: /^[\d\.]*$/ |
| 64 | + number: /^\d*$/ |
| 65 | + tel: /^[0-9/\-\+\s\(\)]*$/ |
| 66 | + email: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ |
| 67 | + |
| 68 | + rules: |
| 69 | + number: (validator, $element, value) -> |
| 70 | + return true if $element.attr('type') != 'number' || !(''+value).length |
| 71 | + validator.config.pattern.number.test(value) |
| 72 | + |
| 73 | + tel: (validator, $element, value) -> |
| 74 | + return true if $element.attr('type') != 'tel' || !(''+value).length |
| 75 | + validator.config.pattern.tel.test(value) |
| 76 | + |
| 77 | + email: (validator, $element, value) -> |
| 78 | + return true if $element.attr('type') != 'email' || !(''+value).length |
| 79 | + validator.config.pattern.email.test(value) |
| 80 | + |
| 81 | + minlength: (validator, $element, value) -> |
| 82 | + return true unless $element.attr('minlength') |
| 83 | + ('' + value).length >= parseInt($element.attr('minlength'), 10) |
| 84 | + |
| 85 | + maxlength: (validator, $element, value) -> |
| 86 | + return true unless $element.attr('maxlength') |
| 87 | + ('' + value).length <= parseInt($element.attr('maxlength'), 10) |
| 88 | + |
| 89 | + required: (validator, $element, value) -> |
| 90 | + return true unless $element.attr('required') |
| 91 | + return false if value == undefined || value == null |
| 92 | + return !!value.length if typeof(value) in ['string', 'array' ] |
| 93 | + !!value |
| 94 | + |
| 95 | + pattern: (validator, $element, value) -> |
| 96 | + return true if !$element.attr('pattern') || !(''+value).length |
| 97 | + (''+value).match($element.attr('pattern')) |
| 98 | + |
| 99 | + hasClass: (validator, $element, value) -> |
| 100 | + return true unless $element.data('rule-has-class') |
| 101 | + $element.hasClass($element.data('rule-has-class')) |
| 102 | + |
| 103 | + decimal: (validator, $element, value) -> |
| 104 | + return true unless $element.data('rule-decimal') || !(''+value).length |
| 105 | + validator.config.pattern.decimal.test(value) |
| 106 | + |
| 107 | + messages: |
| 108 | + generic: 'invalid' |
| 109 | + email: 'invalid email' |
| 110 | + tel: 'invalid phone number' |
| 111 | + number: 'invalid number' |
| 112 | + minlength: 'to short' |
| 113 | + maxlength: 'to long' |
| 114 | + required: 'required' |
| 115 | + hasClass: 'missing class' |
| 116 | + |
| 117 | + handler: |
| 118 | + onValid: null |
| 119 | + onInvalid: null |
| 120 | + onReset: null |
| 121 | + onBuildErrorElement: (validator, $element, value, errors) -> |
| 122 | + error = errors[0] |
| 123 | + $hint = $element.parent().find(validator.config.classes.hint) |
| 124 | + |
| 125 | + unless $hint.length |
| 126 | + $hint = $("<label class='#{validator.config.classes.hint}' " + |
| 127 | + "for='#{$element.attr('id')}'>" + |
| 128 | + error.message + |
| 129 | + "</label>") |
| 130 | + |
| 131 | + $element.data('inputvalidator-hint', $hint) |
| 132 | + $element.after($hint) |
| 133 | + |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +### Dependencies |
| 138 | + * [jquery](https://jquery.com) |
| 139 | + |
| 140 | +### Resources |
| 141 | + * https://github.com/creative-workflow/jquery.input.validator |
| 142 | + * https://travis-ci.org/creative-workflow/jquery.input.validator |
| 143 | + * https://codeclimate.com/github/creative-workflow/jquery.input.validator |
| 144 | + * http://bower.io/search/?q=jquery.input.validator |
| 145 | + |
| 146 | +### Development |
| 147 | +#### Setup |
| 148 | + * `npm install` |
| 149 | + * `bower install` |
| 150 | + * `npm test` |
| 151 | + |
| 152 | +#### Run tests and linter |
| 153 | + * `npm test` |
| 154 | + |
| 155 | +#### Generate build |
| 156 | + * `npm run build` |
| 157 | + |
| 158 | +### Authors |
| 159 | + |
| 160 | + [Tom Hanoldt](https://www.tomhanoldt.info) |
| 161 | + |
| 162 | +## Changelog |
| 163 | +### 1.0.0 |
| 164 | + * initial |
| 165 | + |
| 166 | +# Contributing |
| 167 | + |
| 168 | +Check out the [Contributing Guidelines](CONTRIBUTING.md) |
0 commit comments