class BasicForm extends React.Component { constructor(props) { super(props); this.state = { forename: '', surname: '', email1: '', email2: '', address: '', city: '', postcode: '', tel: '' }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(e) { e.target.classList.add('active'); this.setState({ [e.target.name]: e.target.value }); this.showInputError(e.target.name); } handleSubmit(e) { if(e.type === "click") { e.preventDefault(); return !!this.showFormErrors(); } } showFormErrors() { let isFormValid = true; for (let ref in this.refs) { let node = this.refs[ref]; node.classList.add('active'); const isInputValid = this.showInputError(ref); if (!isInputValid) { isFormValid = false; this.refs[ref].focus(); } } return isFormValid; } showInputError(refName) { const val = this.refs[refName].validity; const errorOnRef = document.getElementById(`${refName}Error`); const isEmail = refName === "email1"; const isEmailConfirm = refName === "email2"; const isTel = refName === "tel"; const isForm = refName === "form"; if (isEmailConfirm) { if (this.refs.email1.value !== this.refs.email2.value) { this.refs.email2.setCustomValidity('Emails do not match'); } else { this.refs.email2.setCustomValidity(''); } } if (val.valueMissing) { this.refs[refName].setCustomValidity(`This field is required`); errorOnRef.textContent = `This field is required`; errorOnRef.style.display = 'block'; } else if (isEmail && val.typeMismatch) { this.refs.email1.setCustomValidity(`Please enter a valid email address`); errorOnRef.textContent = `Please enter a valid email address`; errorOnRef.style.display = 'block'; } else if (isTel && val.patternMismatch) { this.refs.tel.setCustomValidity(`Please provide the telephone number using digits`); errorOnRef.textContent = `Please provide the telephone number using digits`; errorOnRef.style.display = 'block'; } else if (isEmailConfirm && val.customError) { errorOnRef.textContent = `Emails do not match`; errorOnRef.style.display = 'block'; } else if (!isForm) { this.refs[refName].setCustomValidity(''); errorOnRef.textContent = ''; errorOnRef.style.display = 'none'; } return this.refs[refName].checkValidity(); } render() { return (