Skip to content
Snippets Groups Projects
Verified Commit 91b3fe85 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Mail validity control

parent 276c39cf
No related branches found
No related tags found
No related merge requests found
...@@ -26,17 +26,36 @@ export const Form: React.FC = () => { ...@@ -26,17 +26,36 @@ export const Form: React.FC = () => {
interface formData { interface formData {
email: string; email: string;
password: string; password: string;
errors: Array<string>;
} }
const defaultValues: formData = { const defaultValues: formData = {
email: '', email: '',
password: '', password: '',
errors: [],
}; };
const [formValues, setFormValues] = useState<formData>(defaultValues); const [formValues, setFormValues] = useState<formData>(defaultValues);
const isEmailValid: any = () => {
/**
* The following is the RFC 5322 valid email regex
* Hinted from question https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression
* More on http://emailregex.com/
*/
// eslint-disable-next-line
const VALID_EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\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 VALID_EMAIL_REGEX.test(String(formValues.email).toLowerCase());
};
const handleSubmit: any = (event: any) => { const handleSubmit: any = (event: any) => {
event.preventDefault(); event.preventDefault();
console.log(formValues); const errors: Array<string> = [];
if (!isEmailValid()) {
errors.push('email');
console.log('Email address not valid');
} else {
console.log(formValues);
}
}; };
const classes = useStyles(); const classes = useStyles();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment