What is the best way to verify an email address in Vue.js?

Issue with email validation using regex in Vue.js

<div class="input-wrapper">
<div class="email-icon"></div>

<input type="email"  v-model.trim="$v.email.$model"
 v-validate="'required'"
:class="{ 'is-invalid': validationStatus($v.email) }"
 name="email" class=" input-section" 
placeholder="Enter your company email ID"
:maxlength="maxemail"
v-on:keypress="validEmail($event)"
 id='email'  v-model='email' />
 
<div v-if="!$v.email.required" class="invalid-feedback">
The email field is required.</div>
                 
<div v-if="!$v.email.maxLength" class="invalid-feedback-register">
 30 characters only  {{ $v.user.password.$params.maxLength.min }} </div>
</div>

I have encountered an issue while implementing the email validation with regex in Vue.js and so far I have not been able to resolve it.

I need the email field to be mandatory and check for domain names, for example:- [email protected]. I have attempted to implement a keypress event but I am facing difficulties passing the regex as suggested in the provided link.

Answer №1

My go-to for form validation in Vue is Vuelidate. It's incredibly user-friendly and straightforward. With Vuelidate, you can easily set limitations like maxlength and more. Plus, you can even track which parts are 'dirty' in the Vue Devtools! Check out this sample code snippet:

<template>
   <input required="required" v-model="email" :error-messages="emailErrors"
   @input="$v.email.$touch()" @blur="$v.email.$touch()" label="Email*"
   prepend-icon="mdi-email"></input>
</template>

<script>
import { validationMixin } from 'vuelidate'
import { required, email, ... } from 'vuelidate/lib/validators'
import { mapActions } from "vuex";
export default {

mixins: [validationMixin],
validations: {
    ...,
    email: { required, email },
    ...
},
data: () => ({ email: '', ... }),
computed: {
    emailErrors () {
        const errors = []
        if (!this.$v.email.$dirty) return errors
        !this.$v.email.email && errors.push('Must be valid e-mail')
        !this.$v.email.required && errors.push('E-mail is required')
        return errors
    },
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to determine if a jQuery AJAX response contains HTML elements?

On my webpage, I have a single form that triggers an AJAX call which can result in two different responses. One of the responses only includes a status code. In order to display any HTML contents returned by the response object on my page, I need to exami ...

Tips for displaying diverse content in a DIV container when users click on various sections of an image

Apologies for the unclear title, but I'm struggling to explain my technical goal using jargon. The basic concept is this: there will be an image on the webpage, and when a user clicks on a specific area of the image, relevant information will appear ...

Accessing JSON files locally using JavaScript in IE and Firefox

I am a beginner in JavaScript and currently working on a small HTML page that will be run locally. I have a string in JSON format that I need to store and load as a file on the hard drive. I have managed to store the string using the following code snippe ...

inquiry on php function properties

I have a PHP file containing two functions. <?php function first () { //in real case more conditions echo "first"; return true; } function second () { //in real case more conditions echo "second"; return true; } first(); second(); ?> And in an ...

Tips for utilizing two renderCell functions in a datagrid from Material UI without encountering duplication or repetition

Utilizing Material UI, I have designed a table to showcase my data. In this setup, I am required to use renderCell for two specific properties: 'level by user' and 'level by referent'. Issue: The problem arises when Material UI displa ...

Protect database entries from cross-site scripting attacks

I have a project in progress where I am developing an application that extracts text from tweets, saves it to the database, and then presents it on the browser. I am currently concerned about potential security risks if the text contains PHP or HTML tags. ...

What is the best way to send multiple parameter values from jQuery to a Laravel controller?

I am facing an issue with my code where I don't understand how to send multiple parameters from jQuery to the controller. Below is the route: Route::get('/listcdix/{id}/detail/{asnumber}', ['as' => 'remindHelper', & ...

Retrieving JSON information from asynchronous JavaScript and XML (AJAX)

I am struggling with making a URL that contains JSON Data work properly. The issue lies in the data field - I am unsure of what to input here as it is currently undefined. My goal is to store an array of the JSON data received from the ajax request. What ...

Understanding the readability of JavaScript arrays.ORDeciphering

Recently, I've been working with a JSON OBJECT that looks something like this { "kay1": "value1", "key2": "value2", "key3":{ "key31": "value31", "key32": "value32", "key33": "value33" } } However, I am interested in converting it ...

How to ensure that the select option is always at the top of the select dropdown list in a React.js application

Within the select dropdown menu, I have included a default option value of "--select--". As it currently appears at the bottom of the list, I would like it to be displayed at the top instead. Can someone please assist me in achieving this? In the sandbox, ...

Tips for achieving an eye-catching text and image layout on a single page of your Wordpress blog

I've been exploring ways to achieve a design concept for my blog layout. I envision having the text and thumbnail displayed side by side, each taking up 50% of the width until the image reaches its end. Once the image ends, I want the text to span the ...

Once the hidden DIV is revealed, the Youtube video within it begins to load

I currently have a hidden DIV on my website that contains a YouTube clip. My goal is to load the video in the background once the page has fully loaded, so that it's ready to play when the user clicks the button to reveal the DIV. However, at the mo ...

Exploring the translateZ values in Three.js [tutorial]

I've been searching high and low for the answer to my problem, but I can't seem to find a solution anywhere. I'm attempting to determine the position values that would result from an object translating Z by -100, without actually performing ...

Error: The modal function is not recognized by the window.$() method

The index.html file for my Vue.js project looks like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="w ...

Utilize AngularJS to monitor the amount of time users spend within the web application and automatically activate an event at designated intervals

Is there a way to monitor how long a user is active on the website and trigger an event once they reach 30 seconds of browsing time? ...

The beauty of asynchronous GET requests in VueJS

As a newcomer to VueJS, I am exploring how to make a GET request to the GitHub API. Initially, I made a request to sort users by follower count, resulting in an array ordered in descending order of user logins. Following that, I sent another GET request to ...

Creating a text node in a JavaScript application adds HTML content

Is there a way to append an HTML formatted block to an existing div using appendChild without the HTML code itself getting appended? Any tips on how to add HTML design instead of just code with appendChild and createTextNode? Check out this Fiddle <di ...

Access information from an array in Angularjs and transfer it to an identifier

I am facing an issue with passing values from the view to the controller and storing them in an array. My goal is to then retrieve a value from the array and pass it as the id value in the update method. Here is my current setup: HTML <label class="c ...

Utilizing Mongoose Schema Enums Alongside TypeScript Enums

In our Typescript-based NodeJs project utilizing Mongoose, we are seeking the right approach to define an enum field on a Mongoose schema that aligns with a Typescript enum. To illustrate, consider the following enum: enum StatusType { Approved = 1, ...

I found myself pondering the significance of the {blogs: blogs} in my code

app.get("/articles", function(req, res){ Article.find({}, function(err, articles){ if(err){ console.log("an error occurred!!!"); }else{ res.render("homepage", `{articles: articles}`); } }); I created this c ...