diff --git a/dist/aspnet-validation.js b/dist/aspnet-validation.js index 2233d3f..fbc2d5c 100644 --- a/dist/aspnet-validation.js +++ b/dist/aspnet-validation.js @@ -107,759 +107,783 @@ return /******/ (function(modules) { // webpackBootstrap __webpack_require__.r(__webpack_exports__); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "MvcValidationProviders", function() { return MvcValidationProviders; }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "ValidationService", function() { return ValidationService; }); -var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -/** - * Resolves and returns the element referred by original element using ASP.NET selector logic. - * @param elementName - */ -function getRelativeFormElement(elementName, selector) { - // example elementName: Form.PasswordConfirm, Form.Email - // example selector (dafuq): *.Password, *.__RequestVerificationToken - // example result element name: Form.Password, __RequestVerificationToken - var realSelector = selector.substr(2); // Password, __RequestVerificationToken - var objectName = ''; - var dotLocation = elementName.lastIndexOf('.'); - if (dotLocation > -1) { - // Form - objectName = elementName.substr(0, dotLocation); - // Form.Password - var relativeElementName = objectName + '.' + realSelector; - var relativeElement = document.getElementsByName(relativeElementName)[0]; - if (relativeElement) { - return relativeElement; - } - } - // __RequestVerificationToken - return document.getElementsByName(realSelector)[0]; -} -/** - * Contains default implementations for ASP.NET Core MVC validation attributes. - */ -var MvcValidationProviders = /** @class */ (function () { - function MvcValidationProviders() { - /** - * Validates whether the input has a value. - */ - this.required = function (value, element, params) { - return Boolean(value); - }; - /** - * Validates whether the input value satisfies the length contstraint. - */ - this.stringLength = function (value, element, params) { - if (!value) { - return true; - } - if (params.min) { - var min = parseInt(params.min); - if (value.length < min) { - return false; - } - } - if (params.max) { - var max = parseInt(params.max); - if (value.length > max) { - return false; - } - } - return true; - }; - /** - * Validates whether the input value is equal to another input value. - */ - this.compare = function (value, element, params) { - if (!params.other) { - return true; - } - var otherElement = getRelativeFormElement(element.name, params.other); - if (!otherElement) { - return true; - } - return (otherElement.value === value); - }; - /** - * Validates whether the input value is a number within a given range. - */ - this.range = function (value, element, params) { - if (!value) { - return true; - } - var val = parseFloat(value); - if (isNaN(val)) { - return false; - } - if (params.min) { - var min = parseFloat(params.min); - if (val < min) { - return false; - } - } - if (params.max) { - var max = parseFloat(params.max); - if (val > max) { - return false; - } - } - return true; - }; - /** - * Validates whether the input value satisfies a regular expression pattern. - */ - this.regex = function (value, element, params) { - if (!value || !params.pattern) { - return true; - } - var r = new RegExp(params.pattern); - return r.test(value); - }; - /** - * Validates whether the input value is an email in accordance to RFC822 specification, with a top level domain. - */ - this.email = function (value, element, params) { - if (!value) { - return true; - } - // RFC822 email address with .TLD validation - // (c) Richard Willis, Chris Ferdinandi, MIT Licensed - // https://gist.github.com/badsyntax/719800 - // https://gist.github.com/cferdinandi/d04aad4ce064b8da3edf21e26f8944c4 - var r = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*(\.\w{2,})+$/; - return r.test(value); - }; - /** - * Validates whether the input value is a credit card number, with Luhn's Algorithm. - */ - this.creditcard = function (value, element, params) { - if (!value) { - return true; - } - // (c) jquery-validation, MIT Licensed - // https://github.com/jquery-validation/jquery-validation/blob/master/src/additional/creditcard.js - // based on https://en.wikipedia.org/wiki/Luhn_algorithm - // Accept only spaces, digits and dashes - if (/[^0-9 \-]+/.test(value)) { - return false; - } - var nCheck = 0, nDigit = 0, bEven = false, n, cDigit; - value = value.replace(/\D/g, ""); - // Basing min and max length on https://developer.ean.com/general_info/Valid_Credit_Card_Types - if (value.length < 13 || value.length > 19) { - return false; - } - for (n = value.length - 1; n >= 0; n--) { - cDigit = value.charAt(n); - nDigit = parseInt(cDigit, 10); - if (bEven) { - if ((nDigit *= 2) > 9) { - nDigit -= 9; - } - } - nCheck += nDigit; - bEven = !bEven; - } - return (nCheck % 10) === 0; - }; - /** - * Validates whether the input value is a URL. - */ - this.url = function (value, element, params) { - if (!value) { - return true; - } - // (c) Diego Perini, MIT Licensed - // https://gist.github.com/dperini/729294 - var r = new RegExp("^" + - // protocol identifier - "(?:(?:https?|ftp)://)" + - // user:pass authentication - "(?:\\S+(?::\\S*)?@)?" + - "(?:" + - // IP address exclusion - // private & local networks - "(?!(?:10|127)(?:\\.\\d{1,3}){3})" + - "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + - "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + - // IP address dotted notation octets - // excludes loopback network 0.0.0.0 - // excludes reserved space >= 224.0.0.0 - // excludes network & broacast addresses - // (first & last IP address of each class) - "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + - "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + - "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + - "|" + - // host name - "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" + - // domain name - "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" + - // TLD identifier - "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" + - // TLD may end with dot - "\\.?" + - ")" + - // port number - "(?::\\d{2,5})?" + - // resource path - "(?:[/?#]\\S*)?" + - "$", "i"); - return r.test(value); - }; - /** - * Validates whether the input value is a phone number. - */ - this.phone = function (value, element, params) { - if (!value) { - return true; - } - // Allows whitespace or dash as number separator because some people like to do that... - var consecutiveSeparator = /[\+\-\s][\-\s]/g; - if (consecutiveSeparator.test(value)) { - return false; - } - var r = /^\+?[0-9\-\s]+$/; - return r.test(value); - }; - /** - * Asynchronously validates the input value to a JSON GET API endpoint. - */ - this.remote = function (value, element, params) { - if (!value) { - return true; - } - // params.additionalfields: *.Email,*.Username - var fieldSelectors = params.additionalfields.split(','); - var fields = {}; - for (var _i = 0, fieldSelectors_1 = fieldSelectors; _i < fieldSelectors_1.length; _i++) { - var fieldSelector = fieldSelectors_1[_i]; - var fieldName = fieldSelector.substr(2); - var fieldElement = getRelativeFormElement(element.name, fieldSelector); - var hasValue = Boolean(fieldElement && fieldElement.value); - if (!hasValue) { - continue; - } - fields[fieldName] = fieldElement.value; - } - var url = params['url']; - // console.log(fields); - var encodedParams = []; - for (var fieldName in fields) { - var encodedParam = encodeURIComponent(fieldName) + '=' + encodeURIComponent(fields[fieldName]); - encodedParams.push(encodedParam); - } - var payload = encodedParams.join('&'); - // console.log(payload); - return new Promise(function (ok, reject) { - var request = new XMLHttpRequest(); - if (params.type === 'Post') { - var postData = new FormData(); - for (var fieldName in fields) { - postData.append(fieldName, fields[fieldName]); - } - request.open('post', url); - request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - request.send(payload); - } - else { - request.open('get', url + '?' + payload); - request.send(); - } - request.onload = function (e) { - if (request.status >= 200 && request.status < 300) { - var data = JSON.parse(request.responseText); - ok(data); - } - else { - reject({ - status: request.status, - statusText: request.statusText, - data: request.responseText - }); - } - }; - request.onerror = function (e) { - reject({ - status: request.status, - statusText: request.statusText, - data: request.responseText - }); - }; - }); - }; - } - return MvcValidationProviders; -}()); - -/** - * Responsibles for managing the DOM elements and running the validation providers. - */ -var ValidationService = /** @class */ (function () { - function ValidationService() { - /** - * A key-value collection of loaded validation plugins. - */ - this.providers = {}; - /** - * A key-value collection of elements for displaying validation messages for an input (by DOM ID). - */ - this.messageFor = {}; - /** - * A list of managed elements, each having a randomly assigned unique identifier (UID). - */ - this.elementUIDs = []; - /** - * A key-value collection of UID to Element for quick lookup. - */ - this.elementByUID = {}; - /** - * A key-value collection of input UIDs for a
UID. - */ - this.formInputs = {}; - /** - * A key-value map for input UID to its validator factory. - */ - this.validators = {}; - /** - * A key-value map for element UID to its trigger element (submit event for , input event for