@brutalcomponent/react
Version:
Brutalist React components
100 lines (97 loc) • 2.49 kB
JavaScript
;
/**
* @brutalcomponent/react
* (c) David Heffler (https://dvh.sh)
* Licensed under MIT
*/
// src/utils/validation.utils.ts
function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function validatePhone(phone) {
const cleaned = phone.replace(/\D/g, "");
return cleaned.length >= 10 && cleaned.length <= 15;
}
function validateName(name) {
const nameRegex = /^[a-zA-Z\s\-'\.]{2,}$/;
return nameRegex.test(name.trim());
}
function validatePostalCode(code, country = "US") {
const patterns = {
US: /^\d{5}(-\d{4})?$/,
CA: /^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i,
UK: /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i,
GB: /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i,
AU: /^\d{4}$/,
DE: /^\d{5}$/,
FR: /^\d{5}$/,
JP: /^\d{3}-?\d{4}$/
};
return patterns[country]?.test(code) || code.length > 0;
}
function validatePassword(password) {
const errors = [];
if (password.length < 8) {
errors.push("Password must be at least 8 characters");
}
if (!/[a-z]/.test(password)) {
errors.push("Password must contain lowercase letter");
}
if (!/[A-Z]/.test(password)) {
errors.push("Password must contain uppercase letter");
}
if (!/\d/.test(password)) {
errors.push("Password must contain number");
}
if (!/[!@#$%^&*]/.test(password)) {
errors.push("Password must contain special character");
}
return {
isValid: errors.length === 0,
errors
};
}
function validateUrl(url) {
try {
new URL(url);
return true;
} catch {
return false;
}
}
function validateUsername(username) {
const usernameRegex = /^[a-zA-Z0-9_-]{3,20}$/;
return usernameRegex.test(username);
}
/**
* @file src/utils/validation.utils.ts
* @author David (https://dvh.sh)
* @license MIT
*
* @created Thu Sep 11 2025
* @updated Fri Sep 12 2025
*
* @description
* Input validation utilities
*/
/**
* @file src/utils/index.ts
* @author David (https://dvh.sh)
* @license MIT
*
* @created Thu Sep 11 2025
* @updated Sat Sep 13 2025
*
* @description
* Utilities barrel export
*/
exports.validateEmail = validateEmail;
exports.validateName = validateName;
exports.validatePassword = validatePassword;
exports.validatePhone = validatePhone;
exports.validatePostalCode = validatePostalCode;
exports.validateUrl = validateUrl;
exports.validateUsername = validateUsername;
//# sourceMappingURL=chunk-PSSWOWK3.js.map
//# sourceMappingURL=chunk-PSSWOWK3.js.map