React Form Validation
Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
Example#
For custom CoreUI 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.
const [validated, setValidated] = useState(false)const handleSubmit = (event) => { const form = event.currentTarget if (form.checkValidity() === false) { event.preventDefault() event.stopPropagation() } setValidated(true)}return ( <CForm className="row g-3 needs-validation" noValidate validated={validated} onSubmit={handleSubmit} > <CCol md={4}> <CFormInput type="text" defaultValue="Mark" feedbackValid="Looks good!" id="validationCustom01" label="First name" required /> </CCol> <CCol md={4}> <CFormInput type="text" defaultValue="Otto" feedbackValid="Looks good!" id="validationCustom02" label="First name" required /> </CCol> <CCol md={4}> <CFormLabel htmlFor="validationCustomUsername">Username</CFormLabel> <CInputGroup className="has-validation"> <CInputGroupText>@</CInputGroupText> <CFormInput type="text" aria-describedby="inputGroupPrependFeedback" feedbackValid="Please choose a username." id="validationCustomUsername" required /> </CInputGroup> </CCol> <CCol md={6}> <CFormInput type="text" aria-describedby="validationCustom03Feedback" feedbackInvalid="Please provide a valid city." id="validationCustom03" label="City" required /> </CCol> <CCol md={3}> <CFormSelect aria-describedby="validationCustom04Feedback" feedbackInvalid="Please select a valid state." id="validationCustom04" label="State" required > <option disabled>Choose...</option> <option>...</option> </CFormSelect> </CCol> <CCol md={3}> <CFormInput type="text" aria-describedby="validationCustom05Feedback" feedbackInvalid="Please provide a valid zip." id="validationCustom05" label="Zip" required /> </CCol> <CCol xs={12}> <CFormCheck type="checkbox" id="invalidCheck" label="Agree to terms and conditions" required /> <CFormFeedback invalid>You must agree before submitting.</CFormFeedback> </CCol> <CCol xs={12}> <CButton color="primary" type="submit"> Submit form </CButton> </CCol> </CForm>)
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.
<CForm className="row g-3"> <CCol md={4}> <CFormInput type="text" id="validationDefault01" label="First name" defaultValue="Mark" required /> </CCol> <CCol md={4}> <CFormInput type="text" id="validationDefault02" label="Last name" defaultValue="Otto" required /> </CCol> <CCol md={4}> <CFormLabel htmlFor="validationDefaultUsername">Username</CFormLabel> <CInputGroup> <CInputGroupText id="inputGroupPrepend02">@</CInputGroupText> <CFormInput type="text" id="validationDefaultUsername" defaultValue="" aria-describedby="inputGroupPrepend02" required /> </CInputGroup> </CCol> <CCol md={6}> <CFormInput type="text" id="validationDefault03" label="City" required /> </CCol> <CCol md={3}> <CFormSelect id="validationDefault04" label="State"> <option disabled>Choose...</option> <option>...</option> </CFormSelect> </CCol> <CCol md={3}> <CFormInput type="text" id="validationDefault05" label="Zip" required /> </CCol> <CCol xs={12}> <CFormCheck type="checkbox" id="invalidCheck" label="Agree to terms and conditions" required /> </CCol> <CCol xs={12}> <CButton color="primary" type="submit"> Submit form </CButton> </CCol></CForm>
Custom validation#
In case you require custom or 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).
<CForm className="row g-3"> <CCol md={4}> <CFormInput type="text" id="validationServer01" label="Email" feedback="Looks good!" valid required /> </CCol> <CCol md={4}> <CFormInput type="text" id="validationServer02" label="Repeat email" feedback="Looks good!" valid required /> </CCol> <CCol md={4}> <CFormLabel htmlFor="validationServerUsername">Username</CFormLabel> <CInputGroup className="has-validation"> <CInputGroupText id="inputGroupPrepend03">@</CInputGroupText> <CFormInput type="text" id="validationServerUsername" feedback="Please choose a username." defaultValue="" aria-describedby="inputGroupPrepend03" invalid required /> </CInputGroup> </CCol> <CCol md={6}> <CFormInput type="text" id="validationServer03" label="City" feedback="Please provide a valid city." invalid required /> </CCol> <CCol md={3}> <CFormSelect id="validationServer04" label="State" feedback="Please provide a valid city." invalid > <option disabled>Choose...</option> <option>...</option> </CFormSelect> </CCol> <CCol md={3}> <CFormInput type="text" id="validationServer05" label="zip" feedback="Please provide a valid zip." invalid required /> </CCol> <CCol xs={12}> <CFormCheck type="checkbox" id="invalidCheck" label="Agree to terms and conditions" invalid required /> <CFormFeedback invalid>You must agree before submitting.</CFormFeedback> </CCol> <CCol xs={12}> <CButton color="primary" type="submit"> Submit form </CButton> </CCol></CForm>
Supported elements#
Validation styles are available for the following form controls and components:
<CFormCheck>
s<CFormInput>
s<CFormSelect>
s<CFormTextarea>
s
<CForm validated={true}> <div className="mb-3"> <CFormTextarea feedbackInvalid="Please enter a message in the textarea." id="validationTextarea" label="Textarea" placeholder="Required example textarea" required ></CFormTextarea> </div> <CFormCheck className="mb-3" id="validationFormCheck1" label="Check this checkbox" feedbackInvalid="Example invalid feedback text" required /> <CFormCheck type="radio" name="radio-stacked" id="validationFormCheck2" label="Check this checkbox" required /> <CFormCheck className="mb-3" type="radio" name="radio-stacked" id="validationFormCheck3" label="Or toggle this other radio" feedbackInvalid="More example invalid feedback text" required /> <div className="mb-3"> <CFormSelect feedbackInvalid="Example invalid select feedback" aria-label="select example" required > <option selected="" value=""> Open this select menu </option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </CFormSelect> </div> <div className="mb-3"> <CFormInput type="file" id="validationTextarea" feedbackInvalid="Example invalid form file feedback" aria-label="file example" required /> </div> <div className="mb-3"> <CButton type="submit" color="primary" disabled> Submit form </CButton> </div></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.
const [validated, setValidated] = useState(false)const handleSubmit = (event) => { const form = event.currentTarget if (form.checkValidity() === false) { event.preventDefault() event.stopPropagation() } setValidated(true)}return ( <CForm className="row g-3 needs-validation" noValidate validated={validated} onSubmit={handleSubmit}> <CCol md={4} className="position-relative"> <CFormInput type="text" defaultValue="Mark" feedbackValid="Looks good!" id="validationTooltip01" label="First name" required tooltipFeedback /> </CCol> <CCol md={4} className="position-relative"> <CFormInput type="text" defaultValue="Otto" feedbackValid="Looks good!" id="validationTooltip02" label="First name" required tooltipFeedback /> </CCol> <CCol md={4} className="position-relative"> <CFormLabel htmlFor="validationTooltipUsername">Username</CFormLabel> <CInputGroup className="has-validation"> <CInputGroupText id="inputGroupPrepend">@</CInputGroupText> <CFormInput type="text" aria-describedby="inputGroupPrependFeedback" feedbackInvalid="Please choose a username." id="validationTooltipUsername" required tooltipFeedback /> </CInputGroup> </CCol> <CCol md={6} className="position-relative"> <CFormInput type="text" aria-describedby="validationTooltip03Feedback" feedbackInvalid="Please provide a valid city." id="validationTooltip03" label="City" required tooltipFeedback /> </CCol> <CCol md={3} className="position-relative"> <CFormSelect aria-describedby="validationTooltip04Feedback" feedbackInvalid="Please select a valid state." id="validationTooltip04" label="State" required tooltipFeedback > <option selected="" disabled="" value=""> Choose... </option> <option>...</option> </CFormSelect> </CCol> <CCol md={3} className="position-relative"> <CFormInput type="text" aria-describedby="validationTooltip05Feedback" feedbackInvalid="Please provide a valid zip." id="validationTooltip05" label="Zip" required tooltipFeedback /> </CCol> <CCol xs={12} className="position-relative"> <CButton color="primary" type="submit"> Submit form </CButton> </CCol></CForm>)
Customizing#
SASS variables#
$form-feedback-margin-top: $form-text-margin-top !default;$form-feedback-font-size: $form-text-font-size !default;$form-feedback-font-style: $form-text-font-style !default;$form-feedback-valid-color: $success !default;$form-feedback-invalid-color: $danger !default;
$form-feedback-icon-valid-color: $form-feedback-valid-color !default;$form-feedback-icon-valid: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'><path fill='#{$form-feedback-icon-valid-color}' d='M2.3 6.73.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/></svg>") !default;$form-feedback-icon-invalid-color: $form-feedback-invalid-color !default;$form-feedback-icon-invalid: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='#{$form-feedback-icon-invalid-color}'><circle cx='6' cy='6' r='4.5'/><path stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/><circle cx='6' cy='8.2' r='.6' fill='#{$form-feedback-icon-invalid-color}' stroke='none'/></svg>") !default;