Skip to content

Commit bb48d84

Browse files
author
Tom Hanoldt
committed
initial
0 parents  commit bb48d84

20 files changed

+11889
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bower_components
2+
node_modules
3+
.DS_Store
4+
tmp
5+
./grunt

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- "node"
4+
5+
before_script:
6+
- npm install -g grunt-cli bower
7+
- bower install
8+
9+
script: grunt test --verbose

CONTRIBUTING.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
##Issues
2+
3+
- Report issues or feature requests on [GitHub Issues](https://github.com/creative-workflow/jquery.input.validator/issues).
4+
- If reporting a bug, please add a [simplified example](http://sscce.org/).
5+
6+
##Pull requests
7+
- Create a new topic branch for every separate change you make.
8+
- Create a test case if you are fixing a bug or implementing an important feature.
9+
- Make sure the build runs successfully.
10+
11+
## Development
12+
13+
###Tools
14+
We use the following tools for development:
15+
16+
- [Jasmine](http://jasmine.github.io) for tests.
17+
- [NodeJS](http://nodejs.org/download/) required to run grunt.
18+
- [Grunt](http://gruntjs.com/getting-started) for task management.
19+
20+
###Getting started
21+
Install [NodeJS](http://nodejs.org/).
22+
Install globally grunt-cli using the following command:
23+
24+
$ npm install -g grunt-cli bower
25+
26+
Browse to the project root directory and install the dev dependencies:
27+
28+
$ npm install -d && bower install
29+
30+
To execute the build and tests run the following command in the root of the project:
31+
32+
$ grunt
33+
34+
You should see a green message in the console:
35+
36+
Done, without errors.
37+
38+
###Automatic build
39+
You can build automatically after a file change using the following command:
40+
41+
$ grunt watch

Gruntfile.coffee

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict'
2+
3+
module.exports = (grunt) ->
4+
grunt.initConfig
5+
pkg: grunt.file.readJSON 'package.json'
6+
includes:
7+
files:
8+
src: ['src/jquery.input.validator.coffee']
9+
dest: 'tmp'
10+
flatten: true
11+
cwd: '.'
12+
options:
13+
includeRegexp: /^(\s*)#=\s*include\s+(\S+)\s*$/
14+
silent: true
15+
banner: '# <% includes.files.dest %>'
16+
coffee:
17+
compile:
18+
files:
19+
'dist/jquery.input.validator.js': 'tmp/jquery.input.validator.coffee'
20+
'tmp/spec/rules.spec.js': 'spec/rules.spec.coffee'
21+
'tmp/spec/controller.spec.js': 'spec/controller.spec.coffee'
22+
'tmp/spec/reset.spec.js': 'spec/reset.spec.coffee'
23+
'tmp/spec/hint.spec.js': 'spec/hint.spec.coffee'
24+
coffeelint:
25+
app:
26+
[ 'src/*.coffee' ]
27+
uglify:
28+
options:
29+
banner: '/*! <%= pkg.name %> v<%= pkg.version %> | <%= pkg.license %> */\n'
30+
build:
31+
files: 'dist/jquery.input.validator.min.js': 'dist/jquery.input.validator.js'
32+
compress:
33+
main:
34+
options:
35+
mode: 'gzip'
36+
files: [ {
37+
src: [ 'dist/jquery.input.validator.min.js' ]
38+
dest: 'dist/jquery.input.validator.js.gz'
39+
}]
40+
jasmine:
41+
specs:
42+
src: 'dist/jquery.input.validator.js'
43+
options:
44+
specs: 'tmp/spec/*spec.js'
45+
vendor: [
46+
"bower_components/jquery/dist/jquery.min.js"
47+
"bower_components/jasmine-jquery/lib/jasmine-jquery.js"
48+
]
49+
watch:
50+
options: livereload: true
51+
files: '{src,spec}/*.coffee'
52+
tasks: 'default'
53+
54+
# Loading dependencies
55+
for key of grunt.file.readJSON('package.json').devDependencies
56+
if key != 'grunt' and key.indexOf('grunt') == 0
57+
grunt.loadNpmTasks key
58+
59+
grunt.registerTask 'default', [
60+
'includes'
61+
'coffeelint'
62+
'coffee'
63+
'jasmine'
64+
'uglify'
65+
'compress'
66+
]
67+
68+
grunt.registerTask 'test', [
69+
'includes'
70+
'coffee'
71+
'jasmine'
72+
]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Tom Hanoldt, Creative Workflow
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# jquery.input.validator [![Build Status](https://travis-ci.org/creative-workflow/jquery.input.validator.svg?branch=master)](https://travis-ci.org/creative-workflow/jquery.input.validator) [![Code Climate](https://codeclimate.com/github/creative-workflow/jquery.input.validator/badges/gpa.svg)](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)

bower.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "jquery.input.validator",
3+
"version": "1.0.0",
4+
"homepage": "https://github.com/creative-workflow/jquery.input.validator",
5+
"authors": [
6+
"Tom Hanoldt <tom@creative-workflow.berlin>"
7+
],
8+
"description": "This module helps to handle independent input validation based on standart html attributes.",
9+
"main": "src/jquery.input.validator.coffee",
10+
"moduleType": [
11+
"globals"
12+
],
13+
"keywords": [
14+
"jquery",
15+
"validation"
16+
],
17+
"license": "MIT",
18+
"ignore": [
19+
"node_modules",
20+
"bower_components",
21+
"spec"
22+
],
23+
"devDependencies": {
24+
"jasmine-jquery": ">=2.1.0"
25+
},
26+
"dependencies": {
27+
"jquery": ">=1.7.0"
28+
}
29+
}

0 commit comments

Comments
 (0)