├── .gitignore ├── Gruntfile.js ├── LICENSE.md ├── README.md ├── bower.json ├── dist └── angular-validate.min.js ├── index.js ├── package.json └── src └── angular-validate.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | node_modules/ 3 | bower_components/ 4 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function (grunt) { 2 | grunt.initConfig({ 3 | uglify: { 4 | dist: { 5 | files: { 6 | 'dist/angular-validate.min.js': ['src/angular-validate.js'] 7 | } 8 | } 9 | } 10 | }); 11 | 12 | grunt.loadNpmTasks('grunt-contrib-uglify'); 13 | 14 | grunt.registerTask('dist', ['uglify:dist']); 15 | }; 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jan-Paul Kleemans 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Validate 2 | 3 | [![Bower](https://img.shields.io/bower/v/jpkleemans-angular-validate.svg)](https://github.com/jpkleemans/angular-validate/releases/latest) 4 | [![GitHub license](https://img.shields.io/github/license/jpkleemans/angular-validate.svg)](https://github.com/jpkleemans/angular-validate/blob/master/LICENSE.md) 5 | 6 | Painless form validation for [AngularJS](https://github.com/angular/angular.js). Powered by the [jQuery Validation Plugin](https://github.com/jzaefferer/jquery-validation). 7 | 8 | DEMO 9 | 10 | ## Table of contents 11 | 12 | 1. [Installation](#installation) 13 | 2. [Usage](#usage) 14 | 3. [Built-in validation rules](#built-in-validation-rules) 15 | 4. [Configuration](#configuration) 16 | 17 | ## Installation 18 | 19 | Download Angular Validate: 20 | 21 | 22 | - With NPM: 23 | 24 | ```sh 25 | $ npm install jpkleemans-angular-validate 26 | ``` 27 | 28 | - With Bower: 29 | 30 | ```sh 31 | $ bower install jpkleemans-angular-validate 32 | ``` 33 | 34 | - With Git: 35 | 36 | ```sh 37 | $ git clone https://github.com/jpkleemans/angular-validate.git 38 | ``` 39 | 40 | - By manually downloading the [latest release](https://github.com/jpkleemans/angular-validate/releases/latest). 41 | 42 | > When using one of the last two methods make sure you also download the latest release of the [jQuery Validation Plugin](https://github.com/jzaefferer/jquery-validation). 43 | 44 | Include both `jquery.validate.min.js` and `angular-validate.min.js` in your HTML page: 45 | 46 | ```html 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | ``` 55 | 56 | Inject the `ngValidate` module as a dependency into your Angular application: 57 | 58 | ```js 59 | angular.module('myApp', ['ngValidate']); 60 | ``` 61 | 62 | ## Usage 63 | 64 | Add the ng-validate directive to your form and pass the validation options as value: 65 | 66 | ```html 67 |
68 | 69 | 70 |
71 | ``` 72 | 73 | #### Set validation options 74 | 75 | Then set the validation options in your controller: 76 | 77 | ```js 78 | $scope.validationOptions = { 79 | rules: { 80 | email: { 81 | required: true, 82 | email: true 83 | }, 84 | password: { 85 | required: true, 86 | minlength: 6 87 | } 88 | }, 89 | messages: { 90 | email: { 91 | required: "We need your email address to contact you", 92 | email: "Your email address must be in the format of name@domain.com" 93 | }, 94 | password: { 95 | required: "You must enter a password", 96 | minlength: "Your password must have a minimum length of 6 characters" 97 | } 98 | } 99 | } 100 | ``` 101 | 102 | Or (for simple forms) insert the options directly without using a controller: 103 | 104 | ```html 105 |
106 | ``` 107 | 108 | > For all available options, see: http://jqueryvalidation.org/validate#validate-options 109 | 110 | #### Check form validity 111 | 112 | Now you can validate the form by calling `validate()` on the [form instance](https://docs.angularjs.org/guide/forms): 113 | 114 | ```js 115 | $scope.register = function (form) { 116 | if(form.validate()) { 117 | // Form is valid! 118 | } 119 | } 120 | ``` 121 | 122 | > You can also pass your validation options as the first parameter of `validate()`. 123 | 124 | #### Get number of invalid fields 125 | 126 | ```js 127 | $window.alert("There are " + form.numberOfInvalids() + " invalid fields."); 128 | ``` 129 | 130 | ## Built-in validation rules 131 | 132 | A set of standard validation rules is provided by the jQuery Validation Plugin: 133 | 134 | - [required](http://jqueryvalidation.org/required-method) – Makes the element required. 135 | - [remote](http://jqueryvalidation.org/remote-method) – Requests a resource to check the element for validity. 136 | - [minlength](http://jqueryvalidation.org/minlength-method) – Makes the element require a given minimum length. 137 | - [maxlength](http://jqueryvalidation.org/maxlength-method) – Makes the element require a given maxmimum length. 138 | - [rangelength](http://jqueryvalidation.org/rangelength-method) – Makes the element require a given value range. 139 | - [min](http://jqueryvalidation.org/min-method) – Makes the element require a given minimum. 140 | - [max](http://jqueryvalidation.org/max-method) – Makes the element require a given maximum. 141 | - [range](http://jqueryvalidation.org/range-method) – Makes the element require a given value range. 142 | - [email](http://jqueryvalidation.org/email-method) – Makes the element require a valid email. 143 | - [url](http://jqueryvalidation.org/url-method) – Makes the element require a valid url. 144 | - [date](http://jqueryvalidation.org/date-method) – Makes the element require a date. 145 | - [dateISO](http://jqueryvalidation.org/dateISO-method) – Makes the element require an ISO date. 146 | - [number](http://jqueryvalidation.org/number-method) – Makes the element require a decimal number. 147 | - [digits](http://jqueryvalidation.org/digits-method) – Makes the element require digits only. 148 | - [creditcard](http://jqueryvalidation.org/creditcard-method) – Makes the element require a credit card number. 149 | - [equalTo](http://jqueryvalidation.org/equalTo-method) – Requires the element to be the same as another one. 150 | 151 | > More info: http://jqueryvalidation.org/documentation/#link-list-of-built-in-validation-methods 152 | 153 | ## Configuration 154 | 155 | Angular Validate ships with a $validatorProvider, that you can use to configure default validation options and custom validation rules. 156 | 157 | #### Default validation options 158 | 159 | ```js 160 | angular.module('myApp') 161 | .config(function ($validatorProvider) { 162 | $validatorProvider.setDefaults({ 163 | errorElement: 'span', 164 | errorClass: 'help-block' 165 | }); 166 | }); 167 | ``` 168 | 169 | > More info: http://jqueryvalidation.org/jQuery.validator.setDefaults 170 | 171 | #### Custom validation rules 172 | 173 | ```js 174 | angular.module('myApp') 175 | .config(function ($validatorProvider) { 176 | $validatorProvider.addMethod("domain", function (value, element) { 177 | return this.optional(element) || /^http:\/\/mydomain.com/.test(value); 178 | }, "Please specify the correct domain for your documents"); 179 | }); 180 | ``` 181 | 182 | > More info: http://jqueryvalidation.org/jQuery.validator.addMethod 183 | 184 | #### Modify default error messages 185 | 186 | ```js 187 | angular.module('myApp') 188 | .config(function ($validatorProvider) { 189 | $validatorProvider.setDefaultMessages({ 190 | required: "This field is required.", 191 | remote: "Please fix this field.", 192 | email: "Please enter a valid email address.", 193 | url: "Please enter a valid URL.", 194 | date: "Please enter a valid date.", 195 | dateISO: "Please enter a valid date (ISO).", 196 | number: "Please enter a valid number.", 197 | digits: "Please enter only digits.", 198 | creditcard: "Please enter a valid credit card number.", 199 | equalTo: "Please enter the same value again.", 200 | accept: "Please enter a value with a valid extension.", 201 | maxlength: $validatorProvider.format("Please enter no more than {0} characters."), 202 | minlength: $validatorProvider.format("Please enter at least {0} characters."), 203 | rangelength: $validatorProvider.format("Please enter a value between {0} and {1} characters long."), 204 | range: $validatorProvider.format("Please enter a value between {0} and {1}."), 205 | max: $validatorProvider.format("Please enter a value less than or equal to {0}."), 206 | min: $validatorProvider.format("Please enter a value greater than or equal to {0}.") 207 | }); 208 | }); 209 | ``` 210 | 211 | > More info: http://jqueryvalidation.org/jQuery.validator.format 212 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jpkleemans-angular-validate", 3 | "version": "1.1.1", 4 | "description": "Painless form validation for AngularJS. Powered by the jQuery Validation Plugin.", 5 | "authors": [ 6 | "Jan-Paul Kleemans " 7 | ], 8 | "license": "MIT", 9 | "homepage": "https://github.com/jpkleemans/angular-validate", 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/jpkleemans/angular-validate.git" 13 | }, 14 | "main": "./dist/angular-validate.min.js", 15 | "ignore": [ 16 | "**/.*", 17 | "package.json", 18 | "Gruntfile.js" 19 | ], 20 | "dependencies": { 21 | "jquery-validation": "~1.14.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /dist/angular-validate.min.js: -------------------------------------------------------------------------------- 1 | !function(a,b){a.module("ngValidate",[]).directive("ngValidate",function(){return{require:"form",restrict:"A",scope:{ngValidate:"="},link:function(a,c,d,e){var f=c.validate(a.ngValidate);e.validate=function(a){var c=f.settings;f.settings=b.extend(!0,{},f.settings,a);var d=f.form();return f.settings=c,d},e.numberOfInvalids=function(){return f.numberOfInvalids()}}}}).provider("$validator",function(){return b.validator.setDefaults({onsubmit:!1}),{setDefaults:b.validator.setDefaults,addMethod:b.validator.addMethod,setDefaultMessages:function(c){a.extend(b.validator.messages,c)},format:b.validator.format,$get:function(){return{}}}})}(angular,jQuery); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('./src/angular-validate'); 2 | module.exports = 'ngValidate'; 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jpkleemans-angular-validate", 3 | "version": "1.1.3", 4 | "description": "\"jQuery validator plugin for angularjs\"", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/jpkleemans/angular-validate.git" 12 | }, 13 | "keywords": [ 14 | "jQuery", 15 | "validator", 16 | "angularjs" 17 | ], 18 | "author": "jpkleemans", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/jpkleemans/angular-validate/issues" 22 | }, 23 | "homepage": "https://github.com/jpkleemans/angular-validate#readme", 24 | "devDependencies": { 25 | "grunt": "~0.4.5", 26 | "grunt-contrib-uglify": "~0.9.1", 27 | "bower": "~1.8.8" 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/angular-validate.js: -------------------------------------------------------------------------------- 1 | (function (angular, $) { 2 | angular.module('ngValidate', []) 3 | 4 | .directive('ngValidate', function () { 5 | return { 6 | require: 'form', 7 | restrict: 'A', 8 | scope: { 9 | ngValidate: '=' 10 | }, 11 | link: function (scope, element, attrs, form) { 12 | var validator = element.validate(scope.ngValidate); 13 | 14 | form.validate = function (options) { 15 | var oldSettings = validator.settings; 16 | 17 | validator.settings = $.extend(true, {}, validator.settings, options); 18 | 19 | var valid = validator.form(); 20 | 21 | validator.settings = oldSettings; // Reset to old settings 22 | 23 | return valid; 24 | }; 25 | 26 | form.numberOfInvalids = function () { 27 | return validator.numberOfInvalids(); 28 | }; 29 | } 30 | }; 31 | }) 32 | 33 | .provider('$validator', function () { 34 | $.validator.setDefaults({ 35 | onsubmit: false // to prevent validating twice 36 | }); 37 | 38 | return { 39 | setDefaults: $.validator.setDefaults, 40 | addMethod: $.validator.addMethod, 41 | setDefaultMessages: function (messages) { 42 | angular.extend($.validator.messages, messages); 43 | }, 44 | format: $.validator.format, 45 | $get: function () { 46 | return {}; 47 | } 48 | }; 49 | }); 50 | }(angular, jQuery)); 51 | --------------------------------------------------------------------------------