We have the following contact form in React using https://react-bootstrap.github.io/forms/overview/
let contactForm =
(<Form ref={formRef} onSubmit={sendEmail} className='toggle-contact-form'>
<div className='toggle-contact-form__header'>
<p className='p1'>Please Reach out!</p>
<p className='p2'>Use our contact form to reach out with any questions, concerns or issues with the website.</p>
</div>
<Form.Row style={{ paddingTop: 20 }}>
<Form.Group as={Col} controlId='name'>
<Form.Label>Name</Form.Label>
<Form.Control className='cbb-home-input' placeholder='required' />
</Form.Group>
</Form.Row>
<Form.Row>
<Form.Group as={Col} controlId='email'>
<Form.Label>Email Address</Form.Label>
<Form.Control className='cbb-home-input' type='email' placeholder='required' />
</Form.Group>
</Form.Row>
<Form.Row>
<Form.Group as={Col} controlId='phone'>
<Form.Label>Phone Number</Form.Label>
<Form.Control className='cbb-home-input' placeholder='optional' />
</Form.Group>
</Form.Row>
<Form.Row>
<Form.Group as={Col} controlId='message'>
<Form.Label>Message</Form.Label>
<Form.Control className='cbb-home-input' as='textarea' rows='2' placeholder='required' />
</Form.Group>
</Form.Row>
<Form.Row>
<Form.Group as={Col} controlId='button'>
<Button variant='primary' type='submit' disabled={true}>
{isSubmitting ? 'Sending Email...' : 'Submit'}
</Button>
</Form.Group>
</Form.Row>
</Form>);
Currently the button disabled={true}, we'd like to make this conditional on the Form.Control elements for name, message both being not empty, and email being a valid email address. Currently we have no form validation. Is it possible to validate this form as such?
The Bootstrap docs suggest using a library to make this process easier:
But here's how you can do it without a library:
Since we need access to the values of the input fields, we'll need to use controlled components to hold the form data. First we will set up some
useStatevariables to hold the data:Then we need to use those state variables to handle the data in form fields by setting the
valueandonChangeprops:Now that we have access to the form field data, we can create a variable to keep track of whether the user input is valid:
The
checkValidityfunction can check ifname,message, andemailmeet the requirements we want them too:At this point, the
isValidvariable will always be updated with whether or not the current user input in the form is valid. Specifically, we are making surenameandmessageare not empty, and thatemailpasses a simple regex validity check.Finally, we disable the submit button whenever
isValidis false using thedisabledprop:Here's a full working example on CodeSandbox: