Vue 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.
<template>
<CForm
class="row g-3 needs-validation"
novalidate
:validated="validatedCustom01"
@submit="handleSubmitCustom01"
>
<CCol md="4">
<CFormInput
feedbackValid="Looks good!"
id="validationCustom01"
label="First name"
required
value="Mark"
/>
</CCol>
<CCol md="4">
<CFormInput
feedbackValid="Looks good!"
id="validationCustom02"
label="Email" value="Otto"
required
/>
</CCol>
<CCol md="4">
<CFormLabel for="validationCustomUsername">Username</CFormLabel>
<CInputGroup class="has-validation">
<CInputGroupText id="inputGroupPrepend">@</CInputGroupText>
<CFormInput
id="validationCustomUsername"
aria-describedby="inputGroupPrepend"
feedbackInvalid="Please choose a username."
required
/>
</CInputGroup>
</CCol>
<CCol md="6">
<CFormInput
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 selected="" disabled="" value="">
Choose...
</option>
<option>...</option>
</CFormSelect>
</CCol>
<CCol md="3">
<CFormInput
feedbackInvalid="Please provide a valid zip."
id="validationCustom05"
label="Zip"
required
/>
</CCol>
<CCol md="8">
<CDateRangePicker
feedbackInvalid="Please select a valid date range."
id="validationCustom06"
label="Date range"
required
/>
</CCol>
<CCol md="4">
<CTimePicker
feedbackInvalid="Please select a valid time."
id="validationCustom07"
label="Time"
required
/>
</CCol>
<CCol xs="12">
<CFormCheck
feedbackInvalid="You must agree before submitting."
id="invalidCheck"
label="Agree to terms and conditions"
required
type="checkbox"
/>
</CCol>
<CCol xs="12">
<CButton color="primary" type="submit">Submit form</CButton>
</CCol>
</CForm>
</template>
<script>
export default {
data: () => {
return {
validatedCustom01: null,
}
},
methods: {
handleSubmitCustom01(event) {
const form = event.currentTarget
if (form.checkValidity() === false) {
event.preventDefault()
event.stopPropagation()
}
this.validatedCustom01 = true
},
}
}
</script>
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 class="row g-3">
<CCol md="4">
<CFormInput
feedbackValid="Looks good!"
id="validationDefault01"
label="First name"
required
value="Mark"
/>
</CCol>
<CCol md="4">
<CFormInput
feedbackValid="Looks good!"
id="validationDefault02"
label="Email" value="Otto"
required
/>
</CCol>
<CCol md="4">
<CFormLabel for="validationDefaultUsername">Username</CFormLabel>
<CInputGroup class="has-validation">
<CInputGroupText id="inputGroupPrepend">@</CInputGroupText>
<CFormInput
id="validationDefaultUsername"
aria-describedby="inputGroupPrepend"
feedbackInvalid="Please choose a username."
required
/>
</CInputGroup>
</CCol>
<CCol md="6">
<CFormInput
feedbackInvalid="Please provide a valid city."
id="validationDefault03"
label="City"
required
/>
</CCol>
<CCol md="3">
<CFormSelect
aria-describedby="validationDefault04Feedback"
feedbackInvalid="Please select a valid state."
id="validationDefault04"
label="State"
required
>
<option selected="" disabled="" value="">
Choose...
</option>
<option>...</option>
</CFormSelect>
</CCol>
<CCol md="3">
<CFormInput
feedbackInvalid="Please provide a valid zip."
id="validationDefault05"
label="Zip"
required
/>
</CCol>
<CCol xs="12">
<CFormCheck
feedbackInvalid="You must agree before submitting."
id="invalidCheck"
label="Agree to terms and conditions"
required
type="checkbox"
/>
</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 class="row g-3 needs-validation">
<CCol md="4">
<CFormInput
feedback="Looks good!"
id="validationServer01"
label="Email"
required
valid
value="[email protected]"
/>
</CCol>
<CCol md="4">
<CFormInput
feedback="Looks good!"
id="validationServer02"
label="Repeat email"
required
valid
value="[email protected]"
/>
</CCol>
<CCol md="4">
<CFormLabel for="validationServerUsername">Username</CFormLabel>
<CInputGroup class="has-validation">
<CInputGroupText id="inputGroupPrepend03">@</CInputGroupText>
<CFormInput
aria-describedby="inputGroupPrepend03"
feedback="Please choose a username."
id="validationServerUsername"
invalid
required
/>
</CInputGroup>
</CCol>
<CCol md="6">
<CFormInput
feedback="Please provide a valid city."
id="validationServer03"
invalid
label="City"
required
/>
</CCol>
<CCol md="3">
<CFormSelect
feedback="Please provide a valid city."
id="validationServer04"
invalid
label="State"
>
<option disabled>Choose...</option>
<option>...</option>
</CFormSelect>
</CCol>
<CCol md="3">
<CFormInput
feedback="Please provide a valid zip."
id="validationServer05"
invalid
label="zip"
required
/>
</CCol>
<CCol xs="12">
<CFormCheck
feedbackInvalid="You must agree before submitting."
id="invalidCheck"
invalid
label="Agree to terms and conditions"
required
type="checkbox"
/>
</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 class="mb-3">
<CFormTextarea
feedbackInvalid="Please enter a message in the textarea."
id="validationTextarea"
label="Textarea"
placeholder="Required example textarea"
required
></CFormTextarea>
</div>
<CFormCheck
class="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
class="mb-3"
type="radio"
name="radio-stacked"
id="validationFormCheck3"
label="Or toggle this other radio"
feedbackInvalid="More example invalid feedback text"
required
/>
<div class="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 class="mb-3">
<CFormInput
type="file"
id="validationTextarea"
feedbackInvalid="Example invalid form file feedback"
aria-label="file example"
required
/>
</div>
<div class="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.
<template>
<CForm class="row g-3 needs-validation" novalidate :validated="validatedTooltip01" @submit="handleSubmitTooltip01">
<CCol md="4" class="position-relative">
<CFormLabel for="validationTooltip01">Email</CFormLabel>
<CFormInput id="validationTooltip01" value="Mark" required/>
<CFormFeedback tooltip valid>
Looks good!
</CFormFeedback>
</CCol>
<CCol md="4" class="position-relative">
<CFormLabel for="validationTooltip02">Email</CFormLabel>
<CFormInput id="validationTooltip02" value="Otto" required/>
<CFormFeedback tooltip valid>
Looks good!
</CFormFeedback>
</CCol>
<CCol md="4" class="position-relative">
<CFormLabel for="validationTooltipUsername">Username</CFormLabel>
<CInputGroup class="has-validation">
<CInputGroupText id="inputGroupPrepend">@</CInputGroupText>
<CFormInput id="validationTooltipUsername" value="" aria-describedby="inputGroupPrepend" required/>
<CFormFeedback tooltip invalid>
Please choose a username.
</CFormFeedback>
</CInputGroup>
</CCol>
<CCol md="6" class="position-relative">
<CFormLabel for="validationTooltip03">City</CFormLabel>
<CFormInput id="validationTooltip03" required/>
<CFormFeedback tooltip invalid>
Please provide a valid city.
</CFormFeedback>
</CCol>
<CCol md="3" class="position-relative">
<CFormLabel for="validationTooltip04">City</CFormLabel>
<CFormSelect id="validationTooltip04" required>
<option disabled value="">Choose...</option>
<option>...</option>
</CFormSelect>
<CFormFeedback tooltip invalid>
Please provide a valid city.
</CFormFeedback>
</CCol>
<CCol md="3" class="position-relative">
<CFormLabel for="validationTooltip05">City</CFormLabel>
<CFormInput id="validationTooltip05" required/>
<CFormFeedback tooltip invalid>
Please provide a valid zip.
</CFormFeedback>
</CCol>
<CCol xs="12" class="position-relative">
<CButton color="primary" type="submit">Submit form</CButton>
</CCol>
</CForm>
</template>
<script>
export default {
data: () => {
return {
validatedTooltip01: null,
}
},
methods: {
handleSubmitTooltip01(event) {
const form = event.currentTarget
if (form.checkValidity() === false) {
event.preventDefault()
event.stopPropagation()
}
this.validatedTooltip01 = true
}
}
}
</script>
Customizing
CSS variables
These variables are also color mode adaptive, meaning they change color while in dark mode.
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;
SASS mixins
Two mixins are combined, through our loop, to generate our form validation feedback styles.
@mixin form-validation-state-selector($state) {
@if ($state == "valid" or $state == "invalid") {
.was-validated #{if(&, "&", "")}:#{$state},
#{if(&, "&", "")}.is-#{$state} {
@content;
}
} @else {
#{if(&, "&", "")}.is-#{$state} {
@content;
}
}
}
@mixin form-validation-state(
$state,
$color,
$icon,
$tooltip-color: color-contrast($color),
$tooltip-bg-color: rgba($color, $form-feedback-tooltip-opacity),
$focus-box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba($color, $input-btn-focus-color-opacity)
) {
.#{$state}-feedback {
display: none;
width: 100%;
margin-top: $form-feedback-margin-top;
@include font-size($form-feedback-font-size);
font-style: $form-feedback-font-style;
color: $color;
}
.#{$state}-tooltip {
position: absolute;
top: 100%;
z-index: 5;
display: none;
max-width: 100%; // Contain to parent when possible
padding: $form-feedback-tooltip-padding-y $form-feedback-tooltip-padding-x;
margin-top: .1rem;
@include font-size($form-feedback-tooltip-font-size);
line-height: $form-feedback-tooltip-line-height;
color: $tooltip-color;
background-color: $tooltip-bg-color;
@include border-radius($form-feedback-tooltip-border-radius);
}
@include form-validation-state-selector($state) {
~ .#{$state}-feedback,
~ .#{$state}-tooltip {
display: block;
}
}
.form-control {
@include form-validation-state-selector($state) {
border-color: $color;
@if $enable-validation-icons {
@include ltr-rtl("padding-right", $input-height-inner);
background-image: escape-svg($icon);
background-repeat: no-repeat;
@include ltr-rtl-value-only("background-position", right $input-height-inner-quarter center, left $input-height-inner-quarter center);
background-size: $input-height-inner-half $input-height-inner-half;
}
&:focus {
border-color: $color;
box-shadow: $focus-box-shadow;
}
}
}
// stylelint-disable-next-line selector-no-qualifying-type
textarea.form-control {
@include form-validation-state-selector($state) {
@if $enable-validation-icons {
@include ltr-rtl("padding-right", $input-height-inner);
@include ltr-rtl-value-only("background-position", top $input-height-inner-quarter right $input-height-inner-quarter, top $input-height-inner-quarter left $input-height-inner-quarter);
}
}
}
.form-select {
@include form-validation-state-selector($state) {
border-color: $color;
@if $enable-validation-icons {
&:not([multiple]):not([size]),
&:not([multiple])[size="1"] {
@include ltr-rtl("padding-right", $form-select-feedback-icon-padding-end);
background-image: escape-svg($form-select-indicator), escape-svg($icon);
@include ltr-rtl-value-only("background-position", #{$form-select-bg-position, $form-select-feedback-icon-position});
background-size: $form-select-bg-size, $form-select-feedback-icon-size;
}
}
&:focus {
border-color: $color;
box-shadow: $focus-box-shadow;
}
}
}
.form-control-color {
@include form-validation-state-selector($state) {
@if $enable-validation-icons {
width: add($form-color-width, $input-height-inner);
}
}
}
.form-check-input {
@include form-validation-state-selector($state) {
border-color: $color;
&:checked {
background-color: $color;
}
&:focus {
box-shadow: $focus-box-shadow;
}
~ .form-check-label {
color: $color;
}
}
}
.form-check-inline .form-check-input {
~ .#{$state}-feedback {
@include ltr-rtl("margin-left", .5em);
}
}
.input-group {
> .form-control:not(:focus),
> .form-select:not(:focus),
> .form-floating:not(:focus-within) {
@include form-validation-state-selector($state) {
@if $state == "valid" {
z-index: 3;
} @else if $state == "invalid" {
z-index: 4;
}
}
}
}
}
SASS maps
This is the validation Sass map from _variables.scss
. Override or extend this to generate different or additional states.
$form-validation-states: (
"valid": (
"color": $form-feedback-valid-color,
"icon": $form-feedback-icon-valid
),
"invalid": (
"color": $form-feedback-invalid-color,
"icon": $form-feedback-icon-invalid
)
) !default;
Maps of $form-validation-states
can contain three optional parameters to override tooltips and focus styles.
SASS loops
Used to iterate over $form-validation-states
map values to generate our validation styles. Any modifications to the above Sass map will be reflected in your compiled CSS via this loop.
@each $state, $data in $form-validation-states {
@include form-validation-state($state, $data...);
}