CoreUI is an MIT-licensed open source project and is completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing.
You can support our Open Source software development in the following ways:
Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
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.
1const [validated, setValidated] = useState(false)2const handleSubmit = (event) => {3 const form = event.currentTarget4 if (form.checkValidity() === false) {5 event.preventDefault()6 event.stopPropagation()7 }8 setValidated(true)9}10return (11 <CForm12 className="row g-3 needs-validation"13 noValidate14 validated={validated}15 onSubmit={handleSubmit}16 >17 <CCol md={4}>18 <CFormInput19 type="text"20 defaultValue="Mark"21 feedbackValid="Looks good!"22 id="validationCustom01"23 label="First name"24 required25 />26 </CCol>27 <CCol md={4}>28 <CFormInput29 type="text"30 defaultValue="Otto"31 feedbackValid="Looks good!"32 id="validationCustom02"33 label="First name"34 required35 />36 </CCol>37 <CCol md={4}>38 <CFormLabel htmlFor="validationCustomUsername">Username</CFormLabel>39 <CInputGroup className="has-validation">40 <CInputGroupText>@</CInputGroupText>41 <CFormInput42 type="text"43 aria-describedby="inputGroupPrependFeedback"44 feedbackValid="Please choose a username."45 id="validationCustomUsername"46 required47 />48 </CInputGroup>49 </CCol>50 <CCol md={6}>51 <CFormInput52 type="text"53 aria-describedby="validationCustom03Feedback"54 feedbackInvalid="Please provide a valid city."55 id="validationCustom03"56 label="City"57 required58 />59 </CCol>60 <CCol md={3}>61 <CFormSelect62 aria-describedby="validationCustom04Feedback"63 feedbackInvalid="Please select a valid state."64 id="validationCustom04"65 label="State"66 required67 >68 <option disabled>Choose...</option>69 <option>...</option>70 </CFormSelect>71 </CCol>72 <CCol md={3}>73 <CFormInput74 type="text"75 aria-describedby="validationCustom05Feedback"76 feedbackInvalid="Please provide a valid zip."77 id="validationCustom05"78 label="Zip"79 required80 />81 </CCol>82 <CCol xs={12}>83 <CFormCheck84 type="checkbox"85 id="invalidCheck"86 label="Agree to terms and conditions"87 required88 />89 <CFormFeedback invalid>You must agree before submitting.</CFormFeedback>90 </CCol>91 <CCol xs={12}>92 <CButton color="primary" type="submit">93 Submit form94 </CButton>95 </CCol>96 </CForm>97)
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.
1<CForm className="row g-3">2 <CCol md={4}>3 <CFormInput4 type="text"5 id="validationDefault01"6 label="First name"7 defaultValue="Mark"8 required9 />10 </CCol>11 <CCol md={4}>12 <CFormInput13 type="text"14 id="validationDefault02"15 label="Last name"16 defaultValue="Otto"17 required18 />19 </CCol>20 <CCol md={4}>21 <CFormLabel htmlFor="validationDefaultUsername">Username</CFormLabel>22 <CInputGroup>23 <CInputGroupText id="inputGroupPrepend02">@</CInputGroupText>24 <CFormInput25 type="text"26 id="validationDefaultUsername"27 defaultValue=""28 aria-describedby="inputGroupPrepend02"29 required30 />31 </CInputGroup>32 </CCol>33 <CCol md={6}>34 <CFormInput type="text" id="validationDefault03" label="City" required />35 </CCol>36 <CCol md={3}>37 <CFormSelect id="validationDefault04" label="State">38 <option disabled>Choose...</option>39 <option>...</option>40 </CFormSelect>41 </CCol>42 <CCol md={3}>43 <CFormInput type="text" id="validationDefault05" label="Zip" required />44 </CCol>45 <CCol xs={12}>46 <CFormCheck type="checkbox" id="invalidCheck" label="Agree to terms and conditions" required />47 </CCol>48 <CCol xs={12}>49 <CButton color="primary" type="submit">50 Submit form51 </CButton>52 </CCol>53</CForm>
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).
1<CForm className="row g-3">2 <CCol md={4}>3 <CFormInput4 type="text"5 id="validationServer01"6 label="Email"7 feedback="Looks good!"9 valid10 required11 />12 </CCol>13 <CCol md={4}>14 <CFormInput15 type="text"16 id="validationServer02"17 label="Repeat email"18 feedback="Looks good!"20 valid21 required22 />23 </CCol>24 <CCol md={4}>25 <CFormLabel htmlFor="validationServerUsername">Username</CFormLabel>26 <CInputGroup className="has-validation">27 <CInputGroupText id="inputGroupPrepend03">@</CInputGroupText>28 <CFormInput29 type="text"30 id="validationServerUsername"31 feedback="Please choose a username."32 defaultValue=""33 aria-describedby="inputGroupPrepend03"34 invalid35 required36 />37 </CInputGroup>38 </CCol>39 <CCol md={6}>40 <CFormInput41 type="text"42 id="validationServer03"43 label="City"44 feedback="Please provide a valid city."45 invalid46 required47 />48 </CCol>49 <CCol md={3}>50 <CFormSelect51 id="validationServer04"52 label="State"53 feedback="Please provide a valid city."54 invalid55 >56 <option disabled>Choose...</option>57 <option>...</option>58 </CFormSelect>59 </CCol>60 <CCol md={3}>61 <CFormInput62 type="text"63 id="validationServer05"64 label="zip"65 feedback="Please provide a valid zip."66 invalid67 required68 />69 </CCol>70 <CCol xs={12}>71 <CFormCheck72 type="checkbox"73 id="invalidCheck"74 label="Agree to terms and conditions"75 invalid76 required77 />78 <CFormFeedback invalid>You must agree before submitting.</CFormFeedback>79 </CCol>80 <CCol xs={12}>81 <CButton color="primary" type="submit">82 Submit form83 </CButton>84 </CCol>85</CForm>
Validation styles are available for the following form controls and components:
<CFormCheck>
s<CFormInput>
s<CFormSelect>
s<CFormTextarea>
s1<CForm validated={true}>2 <div className="mb-3">3 <CFormTextarea4 feedbackInvalid="Please enter a message in the textarea."5 id="validationTextarea"6 label="Textarea"7 placeholder="Required example textarea"8 required9 ></CFormTextarea>10 </div>11 <CFormCheck12 className="mb-3"13 id="validationFormCheck1"14 label="Check this checkbox"15 feedbackInvalid="Example invalid feedback text"16 required17 />18 <CFormCheck19 type="radio"20 name="radio-stacked"21 id="validationFormCheck2"22 label="Check this checkbox"23 required24 />25 <CFormCheck26 className="mb-3"27 type="radio"28 name="radio-stacked"29 id="validationFormCheck3"30 label="Or toggle this other radio"31 feedbackInvalid="More example invalid feedback text"32 required33 />34 <div className="mb-3">35 <CFormSelect36 feedbackInvalid="Example invalid select feedback"37 aria-label="select example"38 required39 >40 <option selected="" value="">41 Open this select menu42 </option>43 <option value="1">One</option>44 <option value="2">Two</option>45 <option value="3">Three</option>46 </CFormSelect>47 </div>48 <div className="mb-3">49 <CFormInput50 type="file"51 id="validationTextarea"52 feedbackInvalid="Example invalid form file feedback"53 aria-label="file example"54 required55 />56 </div>57 <div className="mb-3">58 <CButton type="submit" color="primary" disabled>59 Submit form60 </CButton>61 </div>62</CForm>
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.
1const [validated, setValidated] = useState(false)2const handleSubmit = (event) => {3 const form = event.currentTarget4 if (form.checkValidity() === false) {5 event.preventDefault()6 event.stopPropagation()7 }8 setValidated(true)9}10return (11 <CForm12 className="row g-3 needs-validation"13 noValidate14 validated={validated}15 onSubmit={handleSubmit}16>17 <CCol md={4} className="position-relative">18 <CFormInput19 type="text"20 defaultValue="Mark"21 feedbackValid="Looks good!"22 id="validationTooltip01"23 label="First name"24 required25 tooltipFeedback26 />27 </CCol>28 <CCol md={4} className="position-relative">29 <CFormInput30 type="text"31 defaultValue="Otto"32 feedbackValid="Looks good!"33 id="validationTooltip02"34 label="First name"35 required36 tooltipFeedback37 />38 </CCol>39 <CCol md={4} className="position-relative">40 <CFormLabel htmlFor="validationTooltipUsername">Username</CFormLabel>41 <CInputGroup className="has-validation">42 <CInputGroupText id="inputGroupPrepend">@</CInputGroupText>43 <CFormInput44 type="text"45 aria-describedby="inputGroupPrependFeedback"46 feedbackInvalid="Please choose a username."47 id="validationTooltipUsername"48 required49 tooltipFeedback50 />51 </CInputGroup>52 </CCol>53 <CCol md={6} className="position-relative">54 <CFormInput55 type="text"56 aria-describedby="validationTooltip03Feedback"57 feedbackInvalid="Please provide a valid city."58 id="validationTooltip03"59 label="City"60 required61 tooltipFeedback62 />63 </CCol>64 <CCol md={3} className="position-relative">65 <CFormSelect66 aria-describedby="validationTooltip04Feedback"67 feedbackInvalid="Please select a valid state."68 id="validationTooltip04"69 label="State"70 required71 tooltipFeedback72 >73 <option selected="" disabled="" value="">74 Choose...75 </option>76 <option>...</option>77 </CFormSelect>78 </CCol>79 <CCol md={3} className="position-relative">80 <CFormInput81 type="text"82 aria-describedby="validationTooltip05Feedback"83 feedbackInvalid="Please provide a valid zip."84 id="validationTooltip05"85 label="Zip"86 required87 tooltipFeedback88 />89 </CCol>90 <CCol xs={12} className="position-relative">91 <CButton color="primary" type="submit">92 Submit form93 </CButton>94 </CCol>95</CForm>96)