arsalandywriter.com

Form Validation in Vue 3 Using Vee-Validate 4: Range and Confirmation

Written on

Form validation plays a crucial role in any application.

In this article, we will explore how to incorporate Vee-Validate 4 into a Vue 3 application for effective form validation.

Range Validation

The between rule is utilized to ensure that the input number falls within a specified range.

To implement this, you can write:

<template>

<Form @submit="submit" v-slot="{ errors }">

<Field name="field" rules="between:1,10" />

<span>{{ errors.field }}</span>

</Form>

</template>

<script>

import { Form, Field, defineRule } from "vee-validate";

import * as rules from "@vee-validate/rules";

Object.keys(rules).forEach((rule) => {

defineRule(rule, rules[rule]);

});

export default {

components: {

Form,

Field,

},

methods: {

onSubmit(values) {

alert(JSON.stringify(values, null, 2));

},

},

};

</script>

The rules property accepts a string to apply the validation rule to the Field component.

Additionally, you can pass an object to the rules property for the same purpose:

<template>

<Form @submit="submit" v-slot="{ errors }">

<Field name="field" :rules="validations" />

<span>{{ errors.field }}</span>

</Form>

</template>

The minimum and maximum values for the range are specified in an array, and both values are mandatory.

Confirmation Validation

The confirmed rule checks if the value entered matches that of another field.

To utilize this, you can write:

<template>

<Form @submit="submit" v-slot="{ errors }">

<Field name="password" type="password" />

<Field name="confirmation" type="password" rules="confirmed:@password" />

<span>{{ errors.confirmation }}</span>

</Form>

</template>

The rules property is set to confirmed:@password, ensuring that the confirmation field matches the password field.

All Field components must reside within a Form component.

Digit Validation

The digits rule ensures that the input is numeric and contains a specified number of digits.

For example, you can write:

<template>

<Form @submit="submit" v-slot="{ errors }">

<Field name="field" rules="digits:3" />

<span>{{ errors.field }}</span>

</Form>

</template>

We validate that the input has precisely 3 digits using the string 'digits:3'.

You can also write:

<template>

<Form @submit="submit" v-slot="{ errors }">

<Field name="field" :rules="validations" />

<span>{{ errors.field }}</span>

</Form>

</template>

Conclusion

Using Vee-Validate 4, we can effortlessly implement various form validation rules in our Vue 3 applications.

More content at **plainenglish.io**

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

# Mastering the Art of Time Management for a Fulfilling Life

Discover an innovative approach to time management that balances productivity with life’s unexpected joys.

The Pinnacle of Temperature: Scientists Reach 4 Trillion Degrees

Discover the astonishing temperatures scientists have achieved in laboratories, surpassing even the Sun's core.

Harnessing Quantum Algorithms for Mindfulness and Relaxation

Discover how quantum-inspired algorithms can enhance mindfulness and relaxation techniques in a tech-driven world.

The Path to Becoming a Spacefaring Civilization: A Future Vision

Exploring our need to become a spacefaring civilization while addressing Earth’s challenges.

Mastering Min and Max Value Retrieval in JavaScript Arrays

Learn how to efficiently find the minimum and maximum property values in a JavaScript array of objects.

Why Niels Bohr Deserves Recognition, But Not in Baseball's Hall of Fame

Niels Bohr's scientific achievements are remarkable, but they don't qualify him for the Baseball Hall of Fame alongside legendary players.

Exploring Bluesky: Is It the Future of Social Media?

Dive into Bluesky Social, a rising platform competing with Twitter, and explore its unique features and community vibes.

Navigating Nutrition: Harvard's Take on WHO Dietary Guidelines

Harvard experts express concerns over WHO's dietary fat guidelines, emphasizing the need for a balanced view on fats and carbs.