Skip to content

Instantly share code, notes, and snippets.

@skatesham
Created October 26, 2022 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skatesham/5332a2f8528251bac82e88c05e126a9d to your computer and use it in GitHub Desktop.
Save skatesham/5332a2f8528251bac82e88c05e126a9d to your computer and use it in GitHub Desktop.
VeeValidate Hellow World
<script lang="ts">
import { Field, Form, ErrorMessage } from 'vee-validate';
export default {
components: {
Form,
Field,
ErrorMessage
},
methods: {
onSubmit(values: any) {
console.log(values);
},
validateEmail(value: any) {
// if the field is empty
if (!value) {
return 'This field is required';
}
// if the field is not a valid email
const regex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (!regex.test(value)) {
return 'This field must be a valid email';
}
// All is good
return true;
},
},
};
</script>
<template>
<div class="about">
<h1>This is an about page</h1>
<Form @submit="onSubmit">
<Field type="email" name="email" :rules="validateEmail"/>
<p>
<ErrorMessage name="email" />
</p>
<button>Sign up for newsletter</button>
</form>
</div>
</template>
<style>
@media (min-width: 1024px) {
.about {
min-height: 100vh;
display: flex;
align-items: center;
}
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment