├── .babelrc ├── .gitignore ├── src ├── images │ └── demo.gif └── js │ ├── demo.js │ └── validator.js ├── gulpfile.js ├── tests ├── blank.js ├── url.js ├── integer.js ├── numeric.js ├── alpha.js ├── email.js ├── dateISO.js ├── phone.js ├── alphaNum.js ├── ValidatorTest.js └── required.js ├── package.json ├── LICENSE.md ├── index.html ├── README.md └── demo └── index.html /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ] 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | node_modules/ 3 | *.log 4 | .nyc_output -------------------------------------------------------------------------------- /src/images/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gocanto/easiest-js-validator/HEAD/src/images/demo.gif -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | require('laravel-elixir-vue-2'); 2 | var elixir = require('laravel-elixir'); 3 | 4 | elixir.config.sourcemaps = false; 5 | elixir.config.assetsPath = 'src'; 6 | 7 | elixir(function(mix) 8 | { 9 | mix.webpack('demo.js', 'dist/easiest-js-validator.js'); 10 | }); -------------------------------------------------------------------------------- /tests/blank.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input can not be empty', t => { 5 | 6 | let output = Validator.make({ "blank": "Making sure there is some data in this field" }, { "blank": "blank" }, { 7 | "blank": "The field is required and does not allow a blank space." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.blank); 11 | }); -------------------------------------------------------------------------------- /tests/url.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is a valid url', t => { 5 | 6 | let output = Validator.make({ "url": "http://g-ocanto.com" }, { "url": "url" }, { 7 | "url": "The field format is invalid" 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.url); 11 | }); 12 | 13 | test('The input is not a valid url', t => { 14 | 15 | let output = Validator.make({ "url": "gocanto" }, { "url": "url" }, { 16 | "url": "The field format is invalid" 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.url); 20 | }); -------------------------------------------------------------------------------- /tests/integer.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input a valid integer', t => { 5 | 6 | let output = Validator.make({ "integer": "32" }, { "integer": "integer" }, { 7 | "integer": "The field must be an integer." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.integer); 11 | }); 12 | 13 | test('The input is not an integer', t => { 14 | 15 | let output = Validator.make({ "integer": "gocanto" }, { "integer": "integer" }, { 16 | "integer": "The field must be an integer." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.integer); 20 | }); -------------------------------------------------------------------------------- /tests/numeric.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is a valid number', t => { 5 | 6 | let output = Validator.make({ "numeric": "3.2" }, { "numeric": "numeric" }, { 7 | "numeric": "The field must be an numeric." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.numeric); 11 | }); 12 | 13 | test('The input is not a valid number', t => { 14 | 15 | let output = Validator.make({ "numeric": "gocanto" }, { "numeric": "numeric" }, { 16 | "numeric": "The field must be a number." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.numeric); 20 | }); -------------------------------------------------------------------------------- /tests/alpha.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input has a valid alpha format', t => { 5 | 6 | let output = Validator.make({ "alpha": "gocanto" }, { "alpha": "alpha" }, { 7 | "alpha": "The field may only contain letters.." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.alpha); 11 | }); 12 | 13 | test('The input does not have a valid aplha format', t => { 14 | 15 | let output = Validator.make({ "alpha": "1234567890" }, { "alpha": "alpha" }, { 16 | "alpha": "The field may only contain letters.." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.alpha); 20 | }); -------------------------------------------------------------------------------- /tests/email.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is a valid email address', t => { 5 | 6 | let output = Validator.make({ "email": "gustavoocanto@gmail.com" }, { "email": "email" }, { 7 | "email": "The field must be a valid email address." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.email); 11 | }); 12 | 13 | test('The input is not a valid email address', t => { 14 | 15 | let output = Validator.make({ "email": "gocanto" }, { "email": "email" }, { 16 | "email": "The field must be a valid email address." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.email); 20 | }); -------------------------------------------------------------------------------- /tests/dateISO.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input has a valid ISO format', t => { 5 | 6 | let output = Validator.make({ "dateISO": "32" }, { "dateISO": "1984-10-18" }, { 7 | "dateISO": "Please enter a valid date with a ISO format." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.dateISO); 11 | }); 12 | 13 | test('The input does not have a valid ISO format', t => { 14 | 15 | let output = Validator.make({ "dateISO": "gocanto" }, { "dateISO": "dateISO" }, { 16 | "dateISO": "Please enter a valid date with a ISO format." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.dateISO); 20 | }); -------------------------------------------------------------------------------- /tests/phone.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is a valid phone number', t => { 5 | 6 | let output = Validator.make({ "phone": "(123) 456-7890" }, { "phone": "phone" }, { 7 | "phone": "The field does not have a valid phone number format." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.phone); 11 | }); 12 | 13 | test('The input is not a valid phone number', t => { 14 | 15 | let output = Validator.make({ "phone": "gocanto*" }, { "phone": "phone" }, { 16 | "phone": "The field does not have a valid phone number format." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.phone); 20 | }); -------------------------------------------------------------------------------- /tests/alphaNum.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is a valid alphanumeric', t => { 5 | 6 | let output = Validator.make({ "alphaNum": "gocanto123" }, { "alphaNum": "alphaNum" }, { 7 | "alphaNum": "The field may only contain letters and numbers." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.alphaNum); 11 | }); 12 | 13 | test('The input is not a valid alphanumeric', t => { 14 | 15 | let output = Validator.make({ "alphaNum": "gocanto123*" }, { "alphaNum": "alphaNum" }, { 16 | "alphaNum": "The field may only contain letters and numbers." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.alphaNum); 20 | }); -------------------------------------------------------------------------------- /tests/ValidatorTest.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The validator is a function', t => { 5 | t.true(typeof Validator == 'function'); 6 | }); 7 | 8 | test('An input is required and has to be formatted as email', t => { 9 | let output = Validator.make({ "email": "gustavoocanto@gmail.com" }, { "email": "required,email" }, { 10 | "email": "The field must be a valid email address." 11 | }); 12 | 13 | t.false(Object.keys(output).length > 0, output.email); 14 | }); 15 | 16 | test('An input is required and has to be formatted as email, but it has some errors', t => { 17 | let output = Validator.make({ "email": "" }, { "email": "required,email" }, { 18 | "email": "The field must be a valid email address.", 19 | "required": "The field field is required." 20 | }); 21 | 22 | t.true(Object.keys(output).length > 0, output.email); 23 | t.is(output.email, "The field must be a valid email address."); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "license": "MIT", 3 | "version": "1.0.8", 4 | "author": "Gustavo Ocanto", 5 | "main": "src/js/validator.js", 6 | "name": "easiest-js-validator", 7 | "description": "JS form validator", 8 | "homepage": "https://github.com/gocanto/easiest-js-validator", 9 | "scripts": { 10 | "prepublish": "gulp", 11 | "test": "nyc ava --verbose" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/gocanto/easiest-js-validator.git" 16 | }, 17 | "keywords": [ 18 | "js", 19 | "form", 20 | "validator" 21 | ], 22 | "devDependencies": { 23 | "ava": "^0.18.0", 24 | "babel-preset-es2015": "^6.22.0", 25 | "babel-register": "^6.22.0", 26 | "gulp": "^3.*", 27 | "gulp-notify": "^2.*", 28 | "laravel-elixir": "6.0.0-11", 29 | "laravel-elixir-vue": "^0.1.4", 30 | "laravel-elixir-vue-2": "^0.2.0", 31 | "laravel-elixir-webpack-official": "^1.0.2", 32 | "nyc": "^10.1.2", 33 | "vue": "^2.*" 34 | }, 35 | "ava": { 36 | "files": [ 37 | "tests/*.js", 38 | "!**/src" 39 | ], 40 | "require": [ 41 | "babel-register" 42 | ], 43 | "babel": "inherit" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Gustavo Ocanto 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/required.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | let Validator = require('./../src/js/validator'); 3 | 4 | test('The input is valid', t => { 5 | 6 | let output = Validator.make({ "required": "Hi there!" }, { "required": "required" }, { 7 | "required": "The field field is required." 8 | }); 9 | 10 | t.false(Object.keys(output).length > 0, output.required); 11 | }); 12 | 13 | test('The input is required', t => { 14 | 15 | let output = Validator.make({ "required": "" }, { "required": "required" }, { 16 | "required": "The field field is required." 17 | }); 18 | 19 | t.true(Object.keys(output).length > 0, output.required); 20 | t.is(output.required, "The field field is required."); 21 | }); 22 | 23 | test('An input object can be required as well', t => { 24 | 25 | let output = Validator.make({ "required": {} }, { "required": "required" }, { 26 | "required": "The field field is required." 27 | }); 28 | 29 | t.true(Object.keys(output).length > 0, output.required); 30 | t.is(output.required, "The field field is required."); 31 | }); 32 | 33 | test('A valid input object', t => { 34 | 35 | let output = Validator.make({ "required": { name: 'gustavo' } }, { "required": "required" }, { 36 | "required": "The field field is required." 37 | }); 38 | 39 | t.false(Object.keys(output).length > 0); 40 | }); -------------------------------------------------------------------------------- /src/js/demo.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import Validator from './validator'; 3 | 4 | window.Vue = Vue; 5 | 6 | new Vue({ 7 | 8 | el: '#demo', 9 | 10 | data: 11 | { 12 | errors: {}, profile: {}, 13 | 14 | rules: { 15 | url: 'url', 16 | dateISO: 'dateISO', 17 | integer: 'integer', 18 | numeric: 'numeric', 19 | blank: 'required,blank', 20 | email: 'required,email', 21 | phone: 'required,phone', 22 | last_name: 'required,alpha', 23 | first_name: 'required,alpha', 24 | alphaNum: 'required,alphaNum' 25 | }, 26 | 27 | messages: { 28 | 'url': 'The field format is invalid.', 29 | 'numeric': 'The field must be a number.', 30 | 'integer': 'The field must be an integer.', 31 | 'required': 'The field field is required.', 32 | 'alpha': 'The field may only contain letters.', 33 | 'email': 'The field must be a valid email address.', 34 | 'dateISO': 'Please enter a valid date with a ISO format.', 35 | 'alphaNum': 'The field may only contain letters and numbers.', 36 | 'phone': 'The field does not have a valid phone number format.', 37 | 'blank': 'The field is required and does not allow blank spaces.' 38 | } 39 | }, 40 | 41 | methods: 42 | { 43 | //submit the form. 44 | submit: function () 45 | { 46 | //check for validation rules. 47 | let validate = Validator.make(this.profile, this.rules, this.messages); 48 | 49 | if (Object.keys(validate).length > 0) 50 | { 51 | this.errors = validate; 52 | return false; 53 | } 54 | 55 | this.errors = []; 56 | 57 | alert('Do something amazing!'); 58 | }, 59 | 60 | //look up possibles validation errors. 61 | hasError: function (key) 62 | { 63 | return typeof this.errors[key] !== 'undefined'; 64 | } 65 | } 66 | 67 | }); -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | easiest-js-validator - demo 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 24 | 25 |
26 | 27 |
28 |
29 | 30 |
31 | 33 | {{ errors.first_name }} 34 |
35 |
36 | 37 |
38 | 39 |
40 | 42 | 43 | {{ errors.last_name }} 44 |
45 |
46 | 47 |
48 | 49 |
50 | 52 | 53 | {{ errors.email }} 54 |
55 |
56 | 57 |
58 | 59 |
60 | 62 | 63 | {{ errors.url }} 64 |
65 |
66 | 67 |
68 | 69 |
70 | 72 | 73 | {{ errors.numeric }} 74 |
75 |
76 | 77 |
78 | 79 |
80 | 82 | 83 | {{ errors.integer }} 84 |
85 |
86 | 87 |
88 | 89 |
90 | 92 | 93 | {{ errors.alphaNum }} 94 |
95 |
96 | 97 |
98 |
 
99 |
100 | 101 |
102 |
103 | 107 |
108 |
109 |
110 | 111 |
112 |
113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easiest JS Validator 2 | 3 | Demo 4 | Version 5 | Downloads 6 | Downloads 7 | License 8 | 9 | It is a simple library ready to pull in into your project. Its goal is to provide an easy way to validate HTML forms without the headache of adapting any other complicated packages. Also, it was written in es2015, so you can get your feet wet with this amazing new way of working with JS. 10 | 11 | # Installation 12 | To install this package you just need to open your console line and type ```npm i easiest-js-validator```. If there is a problem during the installation, you can try again using the ```force param``` as so ```npm i -f easiest-js-validator``` 13 | 14 | 15 | # Gettings started 16 | First of all, you will have to import the library into the file where you are operating. As so, 17 | 18 | ```js 19 | import Validator from 'easiest-js-validator'; 20 | ``` 21 | 22 | Take a look at the example published. 23 | 24 | # illustration 25 | 26 | ![example](https://github.com/gocanto/easiest-js-validator/blob/development/src/images/demo.gif) 27 | 28 | 29 | Also, you will be able to see the online DEMO 30 | 31 | 32 | # Validation rules array 33 | 34 | This array contains all the information about the form fields that you want to be validated where its keys are the same as your form object, As so: 35 | 36 | ```html 37 | 38 | 39 | ``` 40 | 41 | ```js 42 | //rules object 43 | rules: { 44 | first_name: 'required,alpha', 45 | last_name: 'required,alpha', 46 | email: 'required,email', 47 | address: 'required', 48 | } 49 | ``` 50 | Implementation 51 | 52 | 53 | ```js 54 | //form object 55 | profile: { 56 | first_name, 57 | last_name, 58 | email, 59 | address, 60 | } 61 | ``` 62 | Implementation 63 | 64 | 65 | 66 | # Invoke the validator class 67 | At this point, we just have to call the static method ```make``` into the validator class and pass the info which it will operate. As so, 68 | 69 | ```js 70 | let validate = Validator.make(profile, rules, messages); 71 | ``` 72 | 73 | Where messages will be the responsible of bringing the language into the class, in order for it to offer a better output, such as field is required, email is not valid, etc. It is important to know that messages have to meet the same structure as profile object. 74 | 75 | ```js 76 | //messages object example 77 | messages: { 78 | first_name: 'required', 79 | email: 'must have a valid format' 80 | } 81 | ``` 82 | Implementation 83 | 84 | 85 | 86 | # If there were errors 87 | 88 | If there were errors, you will have an associative array using how reference the exactly field that does not meet the rules. as so, 89 | 90 | ```js 91 | errors = [ 92 | first_name: "The field is required.", 93 | last_name: "The field is required.", 94 | email: "The email field must be a valid email address.", 95 | address: "The field is required." 96 | ] 97 | ``` 98 | Implementation 99 | 100 | 101 | 102 | Now, you have access to the validation messages and can proceed as you want. 103 | 104 | # How can I see if my field has errors? 105 | This is easy enough. You only have to check the returned array and show the result on the form in the best way for you. As so, 106 | 107 | ```js 108 | hasError: function (key) 109 | { 110 | return typeof errors[key] !== 'undefined'; 111 | } 112 | 113 | if (hasError('first_name')) { 114 | console.log(errors['first_name']) 115 | } 116 | 117 | ``` 118 | 119 | You can see the demo on DEMO 120 | 121 | 122 | # Features 123 | 124 | You will be able to validate you forms againts any of this rules: 125 | 126 | * url 127 | * integer 128 | * numeric 129 | * alphaNum 130 | * email 131 | * alpha 132 | * required 133 | * digits 134 | * length 135 | * blank 136 | * dateISO 137 | * phoneNumber 138 | 139 | 140 | # Summary 141 | In spite of the demo was written in vuejs, you will be able to pull in the ```validator class under any other js framework``` 142 | 143 | If you have any question, shoot me an email. I will be glad of helping you out. 144 | 145 | # Contributing 146 | 147 | Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities. 148 | 149 | 150 | # License 151 | 152 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 153 | 154 | 155 | # How can I thank you? 156 | Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter? Spread the word! 157 | 158 | 159 | Don't forget to [follow me on twitter](https://twitter.com/gocanto)! 160 | 161 | Thanks! 162 | 163 | Gustavo Ocanto. 164 | gustavoocanto@gmail.com 165 | -------------------------------------------------------------------------------- /src/js/validator.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Validator class 4 | * @author Gustavo Ocanto 5 | */ 6 | 7 | class Validator 8 | { 9 | /** 10 | * Create a new instance. 11 | * 12 | * @param {Object} data 13 | * @param {Object} rules 14 | * @param {Object} messages 15 | */ 16 | constructor(data, rules, messages) 17 | { 18 | //contains the validation errors. 19 | this.errors = []; 20 | 21 | //the data to be checked out. 22 | this.data = data; 23 | 24 | //the rules required. 25 | this.rules = rules; 26 | 27 | //Error messages container. 28 | this.messages = messages; 29 | } 30 | 31 | /** 32 | * Create a new static instance. 33 | * 34 | * @param {Object} data 35 | * @param {Object} rules 36 | * @param {Object} messages 37 | */ 38 | static make(data, rules, messages) 39 | { 40 | let validate = new Validator(data, rules, messages); 41 | 42 | return validate.handle(); 43 | } 44 | 45 | /** 46 | * Walk through the validations rules. 47 | * 48 | * @returns {Object} 49 | */ 50 | handle() 51 | { 52 | let methods = null; 53 | 54 | for (let rule in this.rules) { 55 | methods = this.rules[rule].split(','); 56 | this.evaluate(methods, rule); 57 | } 58 | 59 | return this.errors; 60 | } 61 | 62 | /** 63 | * Evaluate the input against rules. 64 | * 65 | * @param {Array} methods 66 | * @param {String} field 67 | * @returns {Array} 68 | */ 69 | evaluate(methods, field) 70 | { 71 | let value = this.data[field]; 72 | 73 | //We return if the rule required does not exist and the field value is empty. 74 | if (this.isNotRequired(methods, value)) { 75 | return; 76 | } 77 | 78 | for (let method in methods) { 79 | //if the rule required exits and there was an error, the 80 | //stack errors method is called to keep track of rules 81 | //that did not pass the validation. 82 | if (this[methods[method]] && ! this[methods[method]](value)) { 83 | this.stackErrors({ 84 | key: field, //evaluated field. 85 | error: this.messages[methods[method]] 86 | }); 87 | } 88 | } 89 | } 90 | 91 | /** 92 | * Check whether a field is not required and has an empty value. 93 | * 94 | * @param {Array} methods 95 | * @param {String} value 96 | * @return {Boolean} 97 | */ 98 | isNotRequired(methods, value) 99 | { 100 | //if the rule required does not exist and the field value is empty. 101 | if ((methods.indexOf('required') === -1) && this.empty(value)) { 102 | return true; 103 | } 104 | 105 | return false; 106 | } 107 | 108 | /** 109 | * Keep errors tracked out. 110 | * 111 | * @param {Object} data 112 | * @return {Void} 113 | */ 114 | stackErrors(data) 115 | { 116 | if ( ! this.errors.find((error) => error.key == data.key)) { 117 | this.errors[data.key] = data.error; 118 | } 119 | } 120 | 121 | /** 122 | * Check whether the field is blank. 123 | * 124 | * @param {String} value 125 | * @return {Boolean} 126 | */ 127 | empty(value) 128 | { 129 | return value == null || value.length == 0 || value.trim() == ''; 130 | } 131 | 132 | /** 133 | * No blank fields. 134 | * 135 | * @param {String} value 136 | * @return {Boolean} 137 | */ 138 | required(value) 139 | { 140 | //We return if the value happens to be a boolean one, 141 | //like a checkbox or something similar. 142 | if (typeof value == 'boolean') { 143 | return value; 144 | } 145 | 146 | //If the value happens to be an object, we check whether it has some keys in it. 147 | if (typeof value == 'object') { 148 | return Object.keys(value).length > 0; 149 | } 150 | 151 | //If the values is a string, we check whether it has empty spaces and return. 152 | return ! this.empty(value); 153 | } 154 | 155 | /** 156 | * Numeric rule. 157 | * 158 | * @param {String} value 159 | * @return {Boolean} 160 | */ 161 | numeric(value) 162 | { 163 | return (/^-?(?:0$0(?=\d*\.)|[1-9]|0)\d*(\.\d+)?$/).test(value); 164 | } 165 | 166 | /** 167 | * Integer rule. 168 | * 169 | * @param {String} value 170 | * @return {Boolean} 171 | */ 172 | integer(value) 173 | { 174 | return (/^(-?[1-9]\d*|0)$/).test(value); 175 | } 176 | 177 | /** 178 | * Digits rule. 179 | * 180 | * @param {String} value 181 | * @return {Boolean} 182 | */ 183 | digits(value) 184 | { 185 | return (/^[\d() \.\:\-\+#]+$/).test(value); 186 | } 187 | 188 | /** 189 | * Alpha rule. 190 | * 191 | * @param {String} value 192 | * @return {Boolean} 193 | */ 194 | alpha(value) 195 | { 196 | return (/^[a-zA-Z]+$/).test(value); 197 | } 198 | 199 | /** 200 | * Alpha num rule. 201 | * 202 | * @param {String} value 203 | * @return {Boolean} 204 | */ 205 | alphaNum(value) 206 | { 207 | return !(/\W/).test(value); 208 | } 209 | 210 | /** 211 | * Emails rule. 212 | * 213 | * @param {String} value 214 | * @return {Boolean} 215 | */ 216 | email(value) 217 | { 218 | return (/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/).test(value); 219 | } 220 | 221 | /** 222 | * Url rule. 223 | * 224 | * @param {String} value 225 | * @return {Boolean} 226 | */ 227 | url(value) 228 | { 229 | return (/^(https?|ftp|rmtp|mms):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i).test(value); 230 | } 231 | 232 | /** 233 | * Length rule. 234 | * 235 | * @param {String} value 236 | * @return {Boolean} 237 | */ 238 | length(value) 239 | { 240 | return value && value.length == +arg; 241 | } 242 | 243 | /** 244 | * Check whether the values has a blank value. 245 | * 246 | * @param {String} value 247 | * @return {Boolean} 248 | */ 249 | blank(value) 250 | { 251 | return value && value.trim() !== ""; 252 | } 253 | 254 | /** 255 | * Check whether the values has IOS date format. 256 | * 257 | * @param {String} value 258 | * @return {Boolean} 259 | */ 260 | dateISO(value) 261 | { 262 | return (/^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/).test(value); 263 | } 264 | 265 | /** 266 | * Check whether a given value has a valid US phone number format. 267 | * 268 | * @param {String} value 269 | * @return {Boolean} 270 | */ 271 | phone(value) 272 | { 273 | /* 274 | | ---------------------------------------------------------- 275 | | VALID FORMATS 276 | | ---------------------------------------------------------- 277 | | 278 | | 123-456-7890, (123) 456-7890, 123 456 7890 279 | | 123.456.7890, +91 (123) 456-7890 280 | | 281 | */ 282 | 283 | return (/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/).test(value); 284 | } 285 | } 286 | 287 | module.exports = Validator; 288 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | easiest-js-validator - demo 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 24 | 25 |
26 | 27 |
28 |
29 | 30 |
31 | 33 | {{ errors.first_name }} 34 |
35 |
36 | 37 |
38 | 39 |
40 | 42 | 43 | {{ errors.last_name }} 44 |
45 |
46 | 47 |
48 | 49 |
50 | 52 | 53 | {{ errors.email }} 54 |
55 |
56 | 57 |
58 | 59 |
60 | 62 | 63 | {{ errors.url }} 64 |
65 |
66 | 67 |
68 | 69 |
70 | 72 | 73 | {{ errors.numeric }} 74 |
75 |
76 | 77 |
78 | 79 |
80 | 82 | 83 | {{ errors.integer }} 84 |
85 |
86 | 87 |
88 | 89 |
90 | 92 | 93 | {{ errors.alphaNum }} 94 |
95 |
96 | 97 |
98 | 99 |
100 | 102 | 103 | {{ errors.blank }} 104 |
105 |
106 | 107 |
108 | 109 |
110 | 112 | 113 | {{ errors.dateISO }} 114 |
115 |
116 | 117 |
118 | 119 |
120 | 122 | 123 | {{ errors.phone }} 124 |
125 |
126 | 127 | 128 | 129 |
130 |
 
131 |
132 | 133 |
134 |
135 | 139 |
140 |
141 |
142 | 143 |
144 |
145 | 146 | 147 | 148 | 149 | --------------------------------------------------------------------------------