|
| 1 | +// Simple React Validator v0.0.1 | Created By Dockwa | MIT License | 2017 |
| 2 | +;(function(root, factory) { |
| 3 | + if (typeof define === 'function' && define.amd) { |
| 4 | + define(['react'], factory); |
| 5 | + } else if (typeof exports === 'object') { |
| 6 | + module.exports = factory(require('react')); |
| 7 | + } else { |
| 8 | + root.Simple-react-validator = factory(root.React); |
| 9 | + } |
| 10 | +}(this, function(React) { |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
| 14 | + |
| 15 | +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
| 16 | + |
| 17 | +var SimpleReactValidator = function () { |
| 18 | + function SimpleReactValidator() { |
| 19 | + _classCallCheck(this, SimpleReactValidator); |
| 20 | + |
| 21 | + this.fields = []; |
| 22 | + this.showErrors = false; |
| 23 | + this.typeRules = { |
| 24 | + 'required': { message: 'This field is required', rule: function rule(val) { |
| 25 | + return this._runRegex(val, /.+/); |
| 26 | + } }, |
| 27 | + 'required_checkbox': { message: 'You must check the check box', rule: function rule(val) { |
| 28 | + return val === true; |
| 29 | + } }, |
| 30 | + 'email': { message: 'Please enter a valid email address', rule: function rule(val) { |
| 31 | + return this._runRegex(val, /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i); |
| 32 | + } }, |
| 33 | + 'number': { message: 'Please enter a number', rule: function rule(val) { |
| 34 | + return this._runRegex(val, /^\d+$/); |
| 35 | + } }, |
| 36 | + 'alpha_num': { message: 'Please enter only letters or numbers', rule: function rule(val) { |
| 37 | + return this._runRegex(val, /^[A-Z0-9]*$/i); |
| 38 | + } }, |
| 39 | + 'alpha_num_under': { message: 'Please enter only letters or numbers', rule: function rule(val) { |
| 40 | + return this._runRegex(val, /^[A-Z0-9_]*$/i); |
| 41 | + } }, |
| 42 | + 'expiration': { message: 'Please enter a valid expiration date', rule: function rule(val) { |
| 43 | + return this._runRegex(val, /^(([0]?[1-9]{1})|([1]{1}[0-2]{1}))\s?\/\s?\d{2}$/); |
| 44 | + } }, |
| 45 | + 'credit-card': { message: 'Please enter a valid credit card number', rule: function rule(val) { |
| 46 | + return this._runRegex(val, /^\d{4}\s{1}\d{4,6}\s{1}\d{4,5}\s?\d{0,8}$/); |
| 47 | + } }, |
| 48 | + 'min': { message: 'Please enter :REPLACE: or more characters', rule: function rule(val, option) { |
| 49 | + return val.length >= option; |
| 50 | + } }, |
| 51 | + 'max': { message: 'Please enter no more than :REPLACE: characters', rule: function rule(val, option) { |
| 52 | + return val.length <= option; |
| 53 | + } } |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + _createClass(SimpleReactValidator, [{ |
| 58 | + key: 'displayErrors', |
| 59 | + value: function displayErrors(boolean) { |
| 60 | + this.showErrors = boolean || true; |
| 61 | + } |
| 62 | + |
| 63 | + // return true if all fields cleared, false if there is a validation error |
| 64 | + |
| 65 | + }, { |
| 66 | + key: 'allValid', |
| 67 | + value: function allValid() { |
| 68 | + for (var key in this.fields) { |
| 69 | + if (this.fields.hasOwnProperty(key)) { |
| 70 | + if (this.fields[key] === false) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + }, { |
| 78 | + key: 'message', |
| 79 | + value: function message(field, value, typeString, className) { |
| 80 | + this.fields[field] = true; |
| 81 | + var types = typeString.split('|'); |
| 82 | + var parts; |
| 83 | + var lastParts; |
| 84 | + for (var i = 0; i < types.length; i++) { |
| 85 | + // if the validation does not pass the test |
| 86 | + value = typeof value === 'undefined' || value === null ? '' : value; |
| 87 | + parts = types[i].split(':'); |
| 88 | + lastParts = types[i].split(':'); |
| 89 | + lastParts.shift(); |
| 90 | + // pass in the value being tested and any parts after the rule being specified |
| 91 | + if (this.typeRules[parts[0]].rule.apply(this, [value].concat(lastParts)) === false) { |
| 92 | + this.fields[field] = false; |
| 93 | + if (this.showErrors === true) { |
| 94 | + if (parts.length > 1) { |
| 95 | + return this._errorHTML(this.typeRules[parts[0]].message.replace(':REPLACE:', parts[1]), className); |
| 96 | + } else { |
| 97 | + return this._errorHTML(this.typeRules[parts[0]].message, className); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }, { |
| 104 | + key: 'customMessage', |
| 105 | + value: function customMessage(message, className) { |
| 106 | + if (isset(message)) { |
| 107 | + return this._errorHTML(message, className); |
| 108 | + } |
| 109 | + } |
| 110 | + }, { |
| 111 | + key: '_errorHTML', |
| 112 | + value: function _errorHTML(message, className) { |
| 113 | + var name = "form-error-message"; |
| 114 | + if (isset(className)) { |
| 115 | + name += " " + className; |
| 116 | + } |
| 117 | + return React.createElement('div', { className: name }, message); |
| 118 | + } |
| 119 | + }, { |
| 120 | + key: '_runRegex', |
| 121 | + value: function _runRegex(value, regex) { |
| 122 | + return value.toString().match(regex) !== null; |
| 123 | + } |
| 124 | + }]); |
| 125 | + |
| 126 | + return SimpleReactValidator; |
| 127 | +}(); |
| 128 | +return SimpleReactValidator; |
| 129 | +})); |
0 commit comments