Files
Business-credit-asistant/resources/js/Validation/validators.js
2026-06-29 13:00:18 +06:00

33 lines
1.2 KiB
JavaScript
Vendored

export default function useValidators() {
const isEmpty = (fieldName, fieldValue) => {
return !fieldValue ? `The ${fieldName} is required`:"";
}
const minLength = (fieldName, fieldValue, min) => {
return fieldValue.length < min ? `The ${fieldName} field must be at least ${min} characters long` : "";
}
const maxLength = (fieldName, fieldValue, max) => {
return fieldValue.length > max ? `The ${fieldName} cannot be greater than ${max} characters long` : "";
}
const isEmail = (fieldName, fieldValue) => {
let re = /^(([^<>()[\]\\.,;:\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,}))$/;
return !re.test(fieldValue) ? "The input is not a valid " + fieldName + " address" : "";
}
const isNum = (fieldName, fieldValue) => {
let isNum = /^\d+$/.test(fieldValue);
return !isNum ? "The " + fieldName + " field only have numbers" : "";
}
const isInt = (fieldName, fieldValue) => {
let test = /^\d+$/.test(fieldValue);
return !test ? "The " + fieldName + " must be integer" : "";
}
return { isEmpty, minLength, isEmail, isNum,isInt,maxLength}
}