angular-super-validator
Version:
Angular4 Deep Form Validator
168 lines • 6.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var RichValidator = /** @class */ (function () {
function RichValidator() {
}
RichValidator.unBase64 = function (str) {
return atob(str);
};
RichValidator.base64 = function (str) {
return btoa(str);
};
RichValidator.email = function () {
return function (control) {
var regex = /^(([^<>()\[\]\\.,;:\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,}))$/;
var error = { email: false };
if (!control.value || !regex.test(String(control.value).toLowerCase())) {
return error;
}
return null;
};
};
RichValidator.phone = function () {
return function (control) {
var regex = /^[\s()+-]*([0-9][\s()+-]*){6,20}(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i;
if (!control.value || !regex.test(control.value)) {
return { 'phone': false };
}
return null;
};
};
RichValidator.between = function (min, max, inclusive) {
return function (control) {
if (!control.value || !min || !max) {
return { 'between': false };
}
if (inclusive) {
return control.value >= min && control.value <= max ? null : { 'between': false };
}
return control.value > min && control.value < max ? null : { 'between': false };
};
};
RichValidator.creditCard = function () {
return function (control) {
var regex = /[^0-9-\s]+/;
if (!control.value || regex.test(control.value))
return { 'creditCard': false };
var nCheck = 0;
var bEven = false;
var value = control.value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n), nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9)
nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}
return (nCheck % 10) == 0 ? null : { 'creditCard': false };
};
};
RichValidator.prototype.cvv = function (number) {
return function (control) {
if (!control.value || isNaN(control.value) || !number) {
return { 'cvv': false };
}
// AMEX
var re = new RegExp("^3[47]");
if (number.match(re) != null) {
return control.value.length === 4 ? null : { 'cvv': false };
}
return control.value.length === 3 ? null : { 'cvv': false };
};
};
//The value of control.value should not be found in str arg.
//Set strict arg. to true to match upper or lower cases
RichValidator.prototype.notInclude = function (str, strict) {
return function (control) {
if (!control.value || !str) {
return { 'notInclude': false };
}
if (strict) {
return control.value.toLowerCase().indexOf(str.toLowerCase()) === -1 ? null : { 'notInclude': false };
}
return control.value.indexOf(str) === -1 ? null : { 'notInclude': false };
};
};
//The value of control.value should be found in str arg.
//Set strict arg. to true to match upper or lower cases
RichValidator.prototype.includes = function (str, strict) {
return function (control) {
if (!control.value) {
return { 'includes': false };
}
if (strict) {
return str.toLowerCase().includes(control.value.toLowerCase()) ? null : { 'includes': false };
}
return str.includes(control.value) ? null : { 'includes': false };
};
};
//different
//Set strict arg. to true to match upper or lower cases
RichValidator.prototype.different = function (str, strict) {
return function (control) {
if (!control.value) {
return { 'different': false };
}
if (strict) {
return control.value.toLowerCase() !== str.toLowerCase() ? null : { 'different': false };
}
return control.value !== str ? null : { 'different': false };
};
};
//identical
RichValidator.prototype.identical = function (str) {
return function (control) {
if (!control.value) {
return { 'identical': false };
}
return control.value === str ? null : { 'identical': false };
};
};
RichValidator.prototype.greaterThan = function (number, decimalsLen) {
return function (control) {
if (!control.value || !number) {
return { 'greaterThan': false };
}
var decimals = decimalsLen || 0;
var decNum = 0;
var inputNum = parseFloat(parseFloat(control.value).toFixed(decimals));
var controlIndex = control.value.indexOf('.');
if (decimals > 0) {
if (control.value[controlIndex + 1] !== undefined) {
decNum = (control.value.length - 1) - controlIndex;
}
else {
return { 'greaterThan': false };
}
return inputNum > number && decNum <= decimals ? null : { 'lessThan': false };
}
return inputNum > number && controlIndex < 0 ? null : { 'lessThan': false };
};
};
RichValidator.prototype.lessThan = function (number, decimalsLen) {
return function (control) {
if (!control.value || !number) {
return { 'lessThan': false };
}
var decimals = decimalsLen || 0;
var decNum = 0;
var inputNum = parseFloat(parseFloat(control.value).toFixed(decimals));
var controlIndex = control.value.indexOf('.');
if (decimals > 0) {
if (control.value[controlIndex + 1] !== undefined) {
decNum = (control.value.length - 1) - controlIndex;
}
else {
return { 'lessThan': false };
}
return inputNum < number && decNum <= decimals ? null : { 'lessThan': false };
}
return inputNum < number && controlIndex < 0 ? null : { 'lessThan': false };
};
};
return RichValidator;
}());
exports.RichValidator = RichValidator;
//# sourceMappingURL=richValidator.js.map