Validation

Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.

Custom styles#

For custom Bootstrap React form validation messages, you'll need to add the noValidate boolean property to your <CForm>. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript. Try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you. When attempting to submit, you'll see the :invalid and :valid styles applied to your form controls.

Custom feedback styles apply custom colors, borders, focus styles, and background icons to better communicate feedback.

Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please provide a valid city.
Please provide a valid zip.
You must agree before submitting.
1const [validated, setValidated] = useState(false)
2const handleSubmit = (event) => {
3 const form = event.currentTarget
4 if (form.checkValidity() === false) {
5 event.preventDefault()
6 event.stopPropagation()
7 }
8 setValidated(true)
9}
10return (
11 <CForm
12 className="row g-3 needs-validation"
13 noValidate
14 validated={validated}
15 onSubmit={handleSubmit}
16 >
17 <CCol md={4}>
18 <CFormLabel htmlFor="validationCustom01">Email</CFormLabel>
19 <CFormInput type="text" id="validationCustom01" defaultValue="Mark" required />
20 <CFormFeedback valid>Looks good!</CFormFeedback>
21 </CCol>
22 <CCol md={4}>
23 <CFormLabel htmlFor="validationCustom02">Email</CFormLabel>
24 <CFormInput type="text" id="validationCustom02" defaultValue="Otto" required />
25 <CFormFeedback valid>Looks good!</CFormFeedback>
26 </CCol>
27 <CCol md={4}>
28 <CFormLabel htmlFor="validationCustomUsername">Username</CFormLabel>
29 <CInputGroup className="has-validation">
30 <CInputGroupText id="inputGroupPrepend">@</CInputGroupText>
31 <CFormInput
32 type="text"
33 id="validationCustomUsername"
34 defaultValue=""
35 aria-describedby="inputGroupPrepend"
36 required
37 />
38 <CFormFeedback invalid>Please choose a username.</CFormFeedback>
39 </CInputGroup>
40 </CCol>
41 <CCol md={6}>
42 <CFormLabel htmlFor="validationCustom03">City</CFormLabel>
43 <CFormInput type="text" id="validationCustom03" required />
44 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
45 </CCol>
46 <CCol md={3}>
47 <CFormLabel htmlFor="validationCustom04">City</CFormLabel>
48 <CFormSelect id="validationCustom04">
49 <option disabled>Choose...</option>
50 <option>...</option>
51 </CFormSelect>
52 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
53 </CCol>
54 <CCol md={3}>
55 <CFormLabel htmlFor="validationCustom05">City</CFormLabel>
56 <CFormInput type="text" id="validationCustom05" required />
57 <CFormFeedback invalid>Please provide a valid zip.</CFormFeedback>
58 </CCol>
59 <CCol xs={12}>
60 <CFormCheck
61 type="checkbox"
62 id="invalidCheck"
63 label="Agree to terms and conditions"
64 required
65 />
66 <CFormFeedback invalid>You must agree before submitting.</CFormFeedback>
67 </CCol>
68 <CCol xs={12}>
69 <CButton color="primary" type="submit">
70 Submit form
71 </CButton>
72 </CCol>
73 </CForm>
74)

Browser defaults#

Not interested in custom validation feedback messages or writing JavaScript to change form behaviors? All good, you can use the browser defaults. Try submitting the form below. Depending on your browser and OS, you'll see a slightly different style of feedback.

While these feedback styles cannot be styled with CSS, you can still customize the feedback text through JavaScript.

Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please provide a valid city.
Please provide a valid zip.
You must agree before submitting.
1const [validated, setValidated] = useState(false)
2const handleSubmit = (event) => {
3 const form = event.currentTarget
4 if (form.checkValidity() === false) {
5 event.preventDefault()
6 event.stopPropagation()
7 }
8 setValidated(true)
9}
10return (
11 <CForm className="row g-3 needs-validation" validated={validated} onSubmit={handleSubmit}>
12 <CCol md={4}>
13 <CFormLabel htmlFor="validationDefault01">Email</CFormLabel>
14 <CFormInput type="text" id="validationDefault01" defaultValue="Mark" required />
15 <CFormFeedback valid>Looks good!</CFormFeedback>
16 </CCol>
17 <CCol md={4}>
18 <CFormLabel htmlFor="validationDefault02">Email</CFormLabel>
19 <CFormInput type="text" id="validationDefault02" defaultValue="Otto" required />
20 <CFormFeedback valid>Looks good!</CFormFeedback>
21 </CCol>
22 <CCol md={4}>
23 <CFormLabel htmlFor="validationDefaultUsername">Username</CFormLabel>
24 <CInputGroup className="has-validation">
25 <CInputGroupText id="inputGroupPrepend02">@</CInputGroupText>
26 <CFormInput
27 type="text"
28 id="validationDefaultUsername"
29 defaultValue=""
30 aria-describedby="inputGroupPrepend02"
31 required
32 />
33 <CFormFeedback invalid>Please choose a username.</CFormFeedback>
34 </CInputGroup>
35 </CCol>
36 <CCol md={6}>
37 <CFormLabel htmlFor="validationDefault03">City</CFormLabel>
38 <CFormInput type="text" id="validationDefault03" required />
39 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
40 </CCol>
41 <CCol md={3}>
42 <CFormLabel htmlFor="validationDefault04">City</CFormLabel>
43 <CFormSelect id="validationDefault04">
44 <option disabled>Choose...</option>
45 <option>...</option>
46 </CFormSelect>
47 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
48 </CCol>
49 <CCol md={3}>
50 <CFormLabel htmlFor="validationDefault05">City</CFormLabel>
51 <CFormInput type="text" id="validationDefault05" required />
52 <CFormFeedback invalid>Please provide a valid zip.</CFormFeedback>
53 </CCol>
54 <CCol xs={12}>
55 <CFormCheck
56 type="checkbox"
57 id="invalidCheck"
58 label="Agree to terms and conditions"
59 required
60 />
61 <CFormFeedback invalid>You must agree before submitting.</CFormFeedback>
62 </CCol>
63 <CCol xs={12}>
64 <CButton color="primary" type="submit">
65 Submit form
66 </CButton>
67 </CCol>
68 </CForm>
69)

Server side#

We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with invalid and valid boolean properties.

For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using aria-describedby (noting that this attribute allows more than one id to be referenced, in case the field already points to additional form text).

Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please provide a valid city.
Please provide a valid zip.
You must agree before submitting.
1<CForm className="row g-3 needs-validation">
2 <CCol md={4}>
3 <CFormLabel htmlFor="validationServer01">Email</CFormLabel>
4 <CFormInput type="text" id="validationServer01" defaultValue="Mark" valid required />
5 <CFormFeedback valid>Looks good!</CFormFeedback>
6 </CCol>
7 <CCol md={4}>
8 <CFormLabel htmlFor="validationServer02">Email</CFormLabel>
9 <CFormInput type="text" id="validationServer02" defaultValue="Otto" valid required />
10 <CFormFeedback valid>Looks good!</CFormFeedback>
11 </CCol>
12 <CCol md={4}>
13 <CFormLabel htmlFor="validationServerUsername">Username</CFormLabel>
14 <CInputGroup className="has-validation">
15 <CInputGroupText id="inputGroupPrepend03">@</CInputGroupText>
16 <CFormInput
17 type="text"
18 id="validationServerUsername"
19 defaultValue=""
20 aria-describedby="inputGroupPrepend03"
21 invalid
22 required
23 />
24 <CFormFeedback invalid>Please choose a username.</CFormFeedback>
25 </CInputGroup>
26 </CCol>
27 <CCol md={6}>
28 <CFormLabel htmlFor="validationServer03">City</CFormLabel>
29 <CFormInput type="text" id="validationServer03" invalid required />
30 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
31 </CCol>
32 <CCol md={3}>
33 <CFormLabel htmlFor="validationServer04">City</CFormLabel>
34 <CFormSelect id="validationServer04" invalid>
35 <option disabled>Choose...</option>
36 <option>...</option>
37 </CFormSelect>
38 <CFormFeedback invalid>Please provide a valid city.</CFormFeedback>
39 </CCol>
40 <CCol md={3}>
41 <CFormLabel htmlFor="validationServer05">City</CFormLabel>
42 <CFormInput type="text" id="validationServer05" invalid required />
43 <CFormFeedback invalid>Please provide a valid zip.</CFormFeedback>
44 </CCol>
45 <CCol xs={12}>
46 <CFormCheck
47 type="checkbox"
48 id="invalidCheck"
49 label="Agree to terms and conditions"
50 invalid
51 required
52 />
53 <CFormFeedback invalid>You must agree before submitting.</CFormFeedback>
54 </CCol>
55 <CCol xs={12}>
56 <CButton color="primary" type="submit">
57 Submit form
58 </CButton>
59 </CCol>
60</CForm>

Supported elements#

Validation styles are available for the following form controls and components:

  • <CFormCheck>s
  • <CFormInput>s
  • <CFormSelect>s
  • <CFormTextarea>s
Please enter a message in the textarea.
Example invalid feedback text
More example invalid feedback text
Example invalid select feedback
Example invalid form file feedback
1<CForm validated={true}>
2 <div className="mb-3">
3 <CFormLabel htmlFor="validationTextarea" className="form-label">
4 Textarea
5 </CFormLabel>
6 <CFormInput
7 component="textarea"
8 id="validationTextarea"
9 placeholder="Required example textarea"
10 invalid
11 required
12 ></CFormInput>
13 <CFormFeedback invalid>Please enter a message in the textarea.</CFormFeedback>
14 </div>
15
16 <CFormCheck className="mb-3" id="validationFormCheck1" label="Check this checkbox" required />
17 <CFormFeedback invalid>Example invalid feedback text</CFormFeedback>
18
19 <CFormCheck
20 type="radio"
21 name="radio-stacked"
22 id="validationFormCheck2"
23 label="Check this checkbox"
24 required
25 />
26
27 <CFormCheck
28 className="mb-3"
29 type="radio"
30 name="radio-stacked"
31 id="validationFormCheck3"
32 label="Or toggle this other radio"
33 required
34 />
35 <CFormFeedback invalid>More example invalid feedback text</CFormFeedback>
36
37 <div className="mb-3">
38 <CFormSelect required aria-label="select example">
39 <option>Open this select menu</option>
40 <option value="1">One</option>
41 <option value="2">Two</option>
42 <option value="3">Three</option>
43 </CFormSelect>
44 <CFormFeedback invalid>Example invalid select feedback</CFormFeedback>
45 </div>
46
47 <div className="mb-3">
48 <CFormInput type="file" id="validationTextarea" aria-label="file example" required />
49 <CFormFeedback invalid>Example invalid form file feedback</CFormFeedback>
50 </div>
51
52 <div className="mb-3">
53 <CButton type="submit" color="primary" disabled>
54 Submit form
55 </CButton>
56 </div>
57</CForm>

Tooltips#

If your form layout allows it, you can swap the text for the tooltip to display validation feedback in a styled tooltip. Be sure to have a parent with position: relative on it for tooltip positioning. In the example below, our column classes have this already, but your project may require an alternative setup.

Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please provide a valid city.
Please provide a valid zip.
1const [validated, setValidated] = useState(false)
2const handleSubmit = (event) => {
3 const form = event.currentTarget
4 if (form.checkValidity() === false) {
5 event.preventDefault()
6 event.stopPropagation()
7 }
8 setValidated(true)
9}
10return (
11 <CForm
12 className="row g-3 needs-validation"
13 noValidate
14 validated={validated}
15 onSubmit={handleSubmit}
16 >
17 <CCol md={4} className="position-relative">
18 <CFormLabel htmlFor="validationTooltip01">Email</CFormLabel>
19 <CFormInput type="text" id="validationTooltip01" defaultValue="Mark" required />
20 <CFormFeedback tooltip valid>
21 Looks good!
22 </CFormFeedback>
23 </CCol>
24 <CCol md={4} className="position-relative">
25 <CFormLabel htmlFor="validationTooltip02">Email</CFormLabel>
26 <CFormInput type="text" id="validationTooltip02" defaultValue="Otto" required />
27 <CFormFeedback tooltip valid>
28 Looks good!
29 </CFormFeedback>
30 </CCol>
31 <CCol md={4} className="position-relative">
32 <CFormLabel htmlFor="validationTooltipUsername">Username</CFormLabel>
33 <CInputGroup className="has-validation">
34 <CInputGroupText id="inputGroupPrepend">@</CInputGroupText>
35 <CFormInput
36 type="text"
37 id="validationTooltipUsername"
38 defaultValue=""
39 aria-describedby="inputGroupPrepend"
40 required
41 />
42 <CFormFeedback tooltip invalid>
43 Please choose a username.
44 </CFormFeedback>
45 </CInputGroup>
46 </CCol>
47 <CCol md={6} className="position-relative">
48 <CFormLabel htmlFor="validationTooltip03">City</CFormLabel>
49 <CFormInput type="text" id="validationTooltip03" required />
50 <CFormFeedback tooltip invalid>
51 Please provide a valid city.
52 </CFormFeedback>
53 </CCol>
54 <CCol md={3} className="position-relative">
55 <CFormLabel htmlFor="validationTooltip04">City</CFormLabel>
56 <CFormSelect id="validationTooltip04" required>
57 <option disabled value="">
58 Choose...
59 </option>
60 <option>...</option>
61 </CFormSelect>
62 <CFormFeedback tooltip invalid>
63 Please provide a valid city.
64 </CFormFeedback>
65 </CCol>
66 <CCol md={3} className="position-relative">
67 <CFormLabel htmlFor="validationTooltip05">City</CFormLabel>
68 <CFormInput type="text" id="validationTooltip05" required />
69 <CFormFeedback tooltip invalid>
70 Please provide a valid zip.
71 </CFormFeedback>
72 </CCol>
73 <CCol xs={12} className="position-relative">
74 <CButton color="primary" type="submit">
75 Submit form
76 </CButton>
77 </CCol>
78 </CForm>
79)