neumorphic-peripheral
Version:
A lightweight, framework-agnostic JavaScript/TypeScript library for beautiful neumorphic styling
157 lines (156 loc) • 4.82 kB
JavaScript
;
// Add these functions to your existing src/validators/index.ts file
Object.defineProperty(exports, "__esModule", { value: true });
exports.globalValidationManager = void 0;
exports.validatePasswordStrength = validatePasswordStrength;
exports.validateValue = validateValue;
function validatePasswordStrength(password) {
let score = 0;
const feedback = [];
// Length check
if (password.length >= 8) {
score++;
}
else {
feedback.push('Use at least 8 characters');
}
// Lowercase check
if (/[a-z]/.test(password)) {
score++;
}
else {
feedback.push('Add lowercase letters');
}
// Uppercase check
if (/[A-Z]/.test(password)) {
score++;
}
else {
feedback.push('Add uppercase letters');
}
// Numbers check
if (/[0-9]/.test(password)) {
score++;
}
else {
feedback.push('Add numbers');
}
// Special characters check
if (/[^A-Za-z0-9]/.test(password)) {
score++;
}
else {
feedback.push('Add special characters');
}
// Determine strength level
let strength;
if (score <= 1) {
strength = 'weak';
}
else if (score <= 2) {
strength = 'fair';
}
else if (score <= 3) {
strength = 'good';
}
else {
strength = 'strong';
}
return { score, strength, feedback };
}
function validateValue(value, rules) {
return exports.globalValidationManager.validate(value, rules);
}
// Enhanced global validation manager with more methods
exports.globalValidationManager = {
validate(value, rules) {
const errors = [];
// Required validation
if (rules.required) {
const error = validators.required(value);
if (error)
errors.push(error);
}
// Skip other validations if field is empty and not required
if (!value && !rules.required) {
return { isValid: true, errors: [] };
}
// Email validation
if (rules.email) {
const error = validators.email(value);
if (error)
errors.push(error);
}
// Length validations
if (rules.minLength !== undefined) {
const error = validators.minLength(value, rules.minLength);
if (error)
errors.push(error);
}
if (rules.maxLength !== undefined) {
const error = validators.maxLength(value, rules.maxLength);
if (error)
errors.push(error);
}
// Pattern validation
if (rules.pattern) {
const error = validators.pattern(value, rules.pattern);
if (error)
errors.push(error);
}
// Custom validations
if (rules.custom) {
for (const customValidator of rules.custom) {
const error = customValidator(value);
if (error)
errors.push(error);
}
}
return {
isValid: errors.length === 0,
errors
};
},
addRule(name, validator) {
validators[name] = validator;
},
removeRule(name) {
delete validators[name];
},
clearValidation(element) {
// Remove validation classes and attributes
element.classList.remove('np-error');
element.setAttribute('aria-invalid', 'false');
// Find and hide error message if it exists
const errorElement = element.parentElement?.querySelector('.np-error-message');
if (errorElement) {
errorElement.style.display = 'none';
}
},
validate(element, rules) {
const value = element.value || '';
return this.validate(value, rules);
}
};
// Additional validator functions
const additionalValidators = {
url: (value) => {
const urlRegex = /^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/;
return urlRegex.test(value) ? null : 'Please enter a valid URL';
},
phone: (value) => {
const phoneRegex = /^[\+]?[1-9][\d]{0,15}$/;
return phoneRegex.test(value.replace(/\s/g, '')) ? null : 'Please enter a valid phone number';
},
creditCard: (value) => {
const ccRegex = /^[0-9]{13,19}$/;
const cleanValue = value.replace(/\s/g, '');
return ccRegex.test(cleanValue) ? null : 'Please enter a valid credit card number';
},
zipCode: (value) => {
const zipRegex = /^\d{5}(-\d{4})?$/;
return zipRegex.test(value) ? null : 'Please enter a valid ZIP code';
}
};
// Add additional validators to the main validators object
Object.assign(validators, additionalValidators);