form-validate-av
Version:
React Form Validator
30 lines (24 loc) • 752 B
JavaScript
const isEmail = (value) => {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(value) ? null : "Invalid email format";
};
const isRequired = (value) => {
return value ? null : "This field is required";
};
const minLength = (value, length) => {
return value.length >= length ? null : `Must be at least ${length} characters`;
};
const validateForm = (values, rules) => {
let errors = {};
for (const field in rules) {
for (const rule of rules[field]) {
const error = rule(values[field]);
if (error) {
errors[field] = error;
break;
}
}
}
return errors;
};
module.exports = { isEmail, isRequired, minLength, validateForm };