This is how I am setting errors in my react native registration page. The issue is that it is checking the whole email length but I need to impose character limit of 3 before the @ symbol. What is the best way to do this?
I am using Native Base as my UI kit
const validate = () => {
let errors = {};
if (!formData.firstName) errors.firstName = "First name is required";
if (!formData.lastName) errors.lastName = "Last name is required";
if (!formData.email) errors.email = "Email is required";
if (formData.email && formData.email.length < 3) errors.email = "Email is must be 3 characters";
if (!formData.password) errors.password = "Password is required";
if (!formData.confirmPassword)
errors.confirmPassword = "Confirm password is required";
if (formData.password !== formData.confirmPassword)
errors.confirmPassword = "Passwords do not match";
setErrors(errors);
return Object.keys(errors).length === 0;
};
CodePudding user response:
You could use the split
function as follows.
if (formData.email && formData.email.includes('@') && formData.email.split('@')[0].length < 3)